我创建了一个非常简单的mvc4应用程序(visual的默认互联网应用程序)。这是我的RouteConfig课程:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("anyRoute", "content/themes/staticcontent.html", new { controller = "Account", action = "Register" });
routes.IgnoreRoute("content/themes/{filename}.html");
}
}当输入url mySite.com/content/themes/staticcontent.html时,我希望用户被重定向到mySite.com/Account/Register,但我访问上述文件。
此外,除了staticcontent.html之外,我希望限制用户访问上述目录下的任何html文件,但我也可以访问该文件。
发布于 2014-04-03 08:48:23
也许您已经尝试过了,但是如果您将这两种语句倒置如下:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("content/themes/{filename}.html");
routes.MapRoute("anyRoute", "content/themes/staticcontent.html", new { controller = "Account", action = "Register" });
}
}发布于 2014-04-03 09:25:56
默认情况下,ASP.NET MVC路由不处理.html文件。您需要先在IIS中设置它(请参阅http://weblogs.asp.net/jgalloway/archive/2013/08/29/asp-net-mvc-routing-intercepting-file-requests-like-index-html-and-what-it-teaches-about-how-routing-works.aspx),然后再进行路由。
如果它只是一个需要重定向的文件,那么最简单的方法就是在IIS中设置301或302重定向。
https://stackoverflow.com/questions/20657941
复制相似问题