Asp.Net MVC 301 Redirects

后端 未结 5 1055
无人共我
无人共我 2020-12-16 03:00

We used to use ISAPI Re-Write (Infact its still on our server) although it doesnt work with ASP.Net MVC (Somthing to do with \'euxd\' get param).

We need a relaiable

相关标签:
5条回答
  • 2020-12-16 03:25

    To perform a redirect from a non-MVC page to an MVC controller action your best bet is to use a library like UrlRewriting.net or similar which uses an HttpModule to process each request and dispatch it to a specific location.

    Example: Redirect requests for '/faq.asp' to '/faq':

    <add name="faq.asp" virtualUrl="^~/faq.asp([\?#].*)?$"
                destinationUrl="~/faq"
                redirect="Application"
                redirectMode="Permanent"
                ignoreCase="true" />
    

    When you add the HttpModule that powers UrlRewriting.Net to your Web.config, make sure you define it before the UrlRoutingModule which is defined by ASP.NET automatically. Otherwise, ASP.NET will attempt to handle your request by mapping it to a a file or controller and you may experience some unexpected problems as a result.

    <modules runAllManagedModulesForAllRequests="true">
        <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>
        <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </modules>
    
    0 讨论(0)
  • 2020-12-16 03:28

    If you are using IIS7, I would recommend using the official IIS7 URL Rewrite module.

    • Using the URL Rewrite Module
    • Using URL Rewrite Module 2.0
    0 讨论(0)
  • 2020-12-16 03:36

    Implement custom ActionResult. Example: http://www.stum.de/2008/10/22/permanentredirectresult/

    0 讨论(0)
  • 2020-12-16 03:37

    In MVC 3 there are three new redirect methods that can be used in controllers to redirect permanently (produce a 301); as opposed to the 302s (temporary redirect) produced by the MVC 2 redirects.

    • RedirectPermanent
    • RedirectToActionPermanent
    • RedirectToRoutePermanent
    public ActionResult OldAction()
    {
      return RedirectPermanent(urlname);
    }
    

    There is a great tutorial in the Controllers section of these walkthroughs on PluralSight.

    0 讨论(0)
  • 2020-12-16 03:46

    I just blogged about a simple solution that uses ASP.NET MVC and an XML file to store the 301 redirect mappings.

    However, as per Nathan Taylor's answer, if you need to do Regex based mapping, I would suggest using UrlRewriting.Net.

    0 讨论(0)
提交回复
热议问题