asp.net mvc4路由配置

时间:2013-12-18 12:14:10

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我创建了一个非常简单的mvc4应用程序(visual studio的默认互联网应用程序)。 这是我的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");
    }
}

输入网址mySite.com/content/themes/staticcontent.html后,我希望将用户重定向到mySite.com/Account/Register,但我会访问上述文件。

此外,我想限制用户访问除staticcontent.html之外的所提及目录中的任何html文件,但我也可以访问该文件。

2 个答案:

答案 0 :(得分:1)

也许你已经尝试过了,但是如果你颠倒了两个语句会怎么样:

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" });
    }
}

答案 1 :(得分:1)

默认情况下,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重定向最简单。

相关问题