MVC route where path exists

前端 未结 1 989
你的背包
你的背包 2021-02-14 18:42

I\'m trying to create a route that will caption a url like

http://mysite.com/tech/

but I also have an actual directory under the site that is

1条回答
  •  感情败类
    2021-02-14 18:46

    You need to set the RouteExistingFiles property of your RouteCollections object to true. This will make your routing override the physical location if a clash is found.

    routes.MapRoute("Default", "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = "" }
        );
    routes.RouteExistingFiles = true;
    

    To selectively ignore files/extensions you can use :

    routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\.(css|js|gif|jpg)(/.*)?" });
    

    Another approach which might help if you don't like the ignoreRoute approach, is to extend the ViewEngine and override the file exists implementaion with your own rules.

    public class MyViewEngine : RazorViewEngine
    {
        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            //Some Logic to check for file
        }
    }
    
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new CustomViewEngine());
    

    0 讨论(0)
提交回复
热议问题