简体   繁体   中英

How to redirect custom aspx page to mvc action

I have upgrade aspx project to mvc. Now some of my old customer calling url with .aspx page and they are getting 404(not found) in mvc project.

So now I have to redirect .aspx to mvc page.

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. 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

Can you please suggest a best way to do this with minimal code.

Just define an action with route 'bookshop/{pageName}'

Here are examples for 2 scenarios using Route attribute:

In case, you don't want the URL to change:

[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:

[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/' .

In case, you don't have route attribute mapping enabled, add code below in RegisterRoutes method of RouteConfig.cs file:

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

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