简体   繁体   English

如何将自定义 aspx 页面重定向到 mvc 操作

[英]How to redirect custom aspx page to mvc action

I have upgrade aspx project to mvc.我已将 aspx 项目升级到 mvc。 Now some of my old customer calling url with .aspx page and they are getting 404(not found) in mvc project.现在我的一些老客户使用 .aspx 页面调用 url,他们在 mvc 项目中得到 404(未找到)。

So now I have to redirect .aspx to mvc page.所以现在我必须将 .aspx 重定向到 mvc 页面。

Old URL旧网址

www.domain.com/bookshop/pc-58573-53-{product_name}.aspx

New URL新网址

www.domain.com/{product_name}

I am thinking to do via routing mechanism of mvc.我正在考虑通过 mvc 的路由机制来做。 like once this type of url come then it should be call my custom mvc action and in string parameter i will get pc-58573-53-{product_name}.aspx就像一旦这种类型的 url 出现那么它应该调用我的自定义 mvc 操作并且在字符串参数中我将得到 pc-58573-53-{product_name}.aspx

Can you please suggest a best way to do this with minimal code.您能否建议一种使用最少代码执行此操作的最佳方法。

Just define an action with route 'bookshop/{pageName}'只需使用路线'bookshop/{pageName}'定义一个动作

Here are examples for 2 scenarios using Route attribute:以下是使用 Route 属性的 2 个场景的示例:

In case, you don't want the URL to change:如果您不想更改 URL:

[Route("bookshop/{pageName}")]
public ActionResult MyAction(string pageName)
{
   // add logic according to what you receive in pageName property
   return View();
}

or, In case you want to Redirect to a new URL:或者,如果您想重定向到新 URL:

[Route("bookshop/{pageName}")]
public ActionResult MyAction(string pageName)
{
   // Create and use a method to ExtractProductNameFromPageName
   string productName = ExtractProductNameFromPageName(pageName);
   return Response.Redirect("~/" + productName);
}

The parameter 'pageName' here should catch the page name past 'bookshop/' .此处的参数'pageName'应捕获超过'bookshop/'的页面名称。

In case, you don't have route attribute mapping enabled, add code below in RegisterRoutes method of RouteConfig.cs file:如果您没有启用路由属性映射,请在RouteConfig.cs文件的RegisterRoutes方法中添加以下代码:

// enable mapping of routes defined using Route attribute on specific actions.
routes.MapMvcAttributeRoutes();

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

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