asp.net-coreasp.net-core-mvc

How to ignore routes in ASP.NET Core?


Previously, one would add something like this to Global.aspx.cs, which is gone in .NET Core:

  routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

Here's what I currently have in my Startup.cs (for .NET Core):

  app.UseDefaultFiles();

  app.UseStaticFiles();

  app.UseMvc(routes =>
  {
      routes.MapRoute(
          name: "default",
          template: "{controller=Home}/{action=Index}/{id?}");

      routes.MapSpaFallbackRoute(
          name: "spa-fallback",
          defaults: new { controller = "Home", action = "Index" });
  });

The problem is that in MVC (pre-Core) routes was a RouteCollection and in .NET Core it's a Microsoft.AspNetCore.Routing.IRouteBuilder so IgnoreRoute is not a valid method.


Solution

  • If you want to make a static file accessible without the routing condition, simply use the build-in StaticFiles Middleware. Activate it with app.UseStaticFiles(); in Configure Method and put your static files in wwwroot directory. They're availible on HOST/yourStaticFile

    For more information, refer here