user2966918
user2966918

Reputation: 1

Route specific page to controller in ASP.NET MVC

If I have a page on my site (extension could be ".html", whatever:

http://tempuri.org/mypage.asp

I want this page to invoke a specific web api controller and action. Can anyone help with configuring this in the route mapping? Thanks.

Upvotes: 0

Views: 1027

Answers (1)

vbn
vbn

Reputation: 799

You can route a specific file type to an action of a controller.

Firstly, you have to add a handler in web.config (web.server/handlers node)

<add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

Then, insert the following code in your RegisterRoutes function (it has to be before the default route)

routes.MapRoute(
           name: "Special",
           url: "{page}.html",
           defaults: new { controller = "Home", action = "Index", page = UrlParameter.Optional }
);

After doing so, all requests for .html files will be forwarded to the index action in the home controller.

Hope this helps.

Upvotes: 1

Related Questions