简体   繁体   English

使用aspx扩展名将URL路由到MVC路由

[英]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 我有一个网站将数据发布到旧的Web应用程序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". 我正在使用asp.net mvc编写一个新的应用程序,我希望将相同的url发布应用于我的控制器“ Processor”上一个名为“ Confirm”的操作。 Is it possible to do this using Routing in asp.net mvc? 是否可以使用asp.net mvc中的“路由”来执行此操作? if so how? 如果是这样怎么办?

There is a workaround. 有一种解决方法。

You can add below request handlers in the web.config. 您可以在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. 在本博客中得到了很好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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