简体   繁体   中英

Route url with aspx extension to mvc route

I have a web site posting data to an old web app url mywebsite.com/Confirm.aspx

I am writing a new app using asp.net mvc and i would like that same url post to apply to an action called "Confirm" on my controller "Processor". Is it possible to do this using Routing in asp.net mvc? if so how?

There is a workaround.

You can add below request handlers in the web.config.

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

    <add name="AspxlFileHandler" path="*.aspx" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>

Then you can create route definitions:

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

routes.MapRoute(
    name: "LegacyAspx",
    url: "{page}.aspx",
    defaults: new { controller = "Home", action = "LegacyPage", page = UrlParameter.Optional }
);

Next, you can either handle request in the controller action mentioned in routes.

Or you can further redirect the actions to other pages / sites based on value of Page path variable.

This is explained beautifully in this blog.

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