简体   繁体   中英

asp.net mvc4 route configuration

I've create a very simple mvc4 application(visual studio's default internet application). This is my RouteConfig class:

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

When the url mySite.com/content/themes/staticcontent.html is entered I want the user to be redirected to mySite.com/Account/Register but I access the mentioned file.

Also I want to restrict users from accessing any html file at the mentioned directory except staticcontent.html but I can access that file too.

Maybe you have tried it already, but what if you invert the two statements like:

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

By default ASP.NET MVC routing does not handle .html files. You would need to set this up in IIS first (see 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 ) and then then routing would work.

If it is just a single file that needs to be redirected, it would be simplest to just set up a 301 or 302 redirect in IIS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM