Ali.Rashidi
Ali.Rashidi

Reputation: 1462

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.

Upvotes: 0

Views: 261

Answers (2)

Judo
Judo

Reputation: 5247

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.

Upvotes: 1

Stefano Magistri
Stefano Magistri

Reputation: 1150

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

Upvotes: 1

Related Questions