简体   繁体   English

像“ home.asp”这样的网址在mvc中重定向

[英]urls like “home.asp” redirect in mvc

I need to rebuild a website (in old they use classic ASP but they want to make it now MVC 4) and they dont want to change the urls. 我需要重建一个网站(以前他们使用经典的ASP,但现在想将其制作为MVC 4),并且他们不想更改URL。 for example if the search screen's url is blabla.com/search.asp they want to keep it like this. 例如,如果搜索屏幕的网址是blabla.com/search.asp,则他们希望保留该网址。 But mvc doesn't allow to make urls like "search.asp". 但是mvc不允许创建类似“ search.asp”的网址。 I want to keep url like this but render search View. 我想保留这样的网址,但呈现搜索视图。 Am I need to do this all one by one or there is a dynamic way for it? 我需要一一完成这一切吗,还是有一种动态的方法呢?

for example 例如

requested url = "blabla.com/string variable" 
if variable.Substring(variable.Length - 4) == ".asp";
return View("variable.Substring(0, (variable.Length - 4))")

Note: Syntax is all wrong, I know. 注意:语法是错误的,我知道。 I just tried to explain the condition.. 我只是想解释一下情况。

Note2: They want this because of "SEO" things. 注意2:由于“ SEO”问题,他们希望这样做。 they don't want to lose their ratings. 他们不想失去评分。 any method that doesn't change anything for google, they will accept that method I guess. 不会为Google更改任何方法的任何方法,我都会接受该方法。

You need two things. 您需要两件事。

  1. Define a route for *.asp 为* .asp定义路由
  2. Add an handler for *.asp 为* .asp添加处理程序

RouteConfig 路由配置

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "DefaultAsp",
        url: "{controller}/{action}.asp/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Handler (WebConfig) 处理程序(WebConfig)

(This one needs to be inserted inside /system.webServer/handlers (此代码需要插入/system.webServer/handlers

  <add name="AspFileHandler" path="*.asp" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

Doing this you are also making all URL's built with MVC available with .asp, that means that if you have an anchor that calls another View, that view will have the normal mvc URL with .asp suffix. 这样做还可以使所有使用MVC构建的URL在.asp中可用,这意味着,如果您拥有调用另一个View的锚,则该视图将具有带有.asp后缀的普通mvc URL。

Note 注意

This is generic, you only need to add this line once. 这是通用的,您只需添加一次此行。 Home/Index displayed are just the default Controller and Action. 显示的主目录/索引只是默认的控制器和操作。

Note 2 笔记2

Forgot to explain the handler. 忘了解释处理程序。

You need it because IIS thinks you are asking for an ASP file an it'll try to reach that page and return an error since it doesn't exist. 之所以需要它,是因为IIS认为您正在请求一个ASP文件,它会尝试到达该页面并返回错误,因为它不存在。

With this handler you are allowing your application to handle those pages itself. 使用此处理程序,您可以允许应用程序自己处理这些页面。

MVC does allows you to write a Route containing an extension, and pointing it to a specific Controller: MVC确实允许您编写包含扩展的Route,并将其指向特定的Controller:

In RouteConfig.cs: 在RouteConfig.cs中:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute("Test", "test.asp", new {controller = "Test", action = "test" });
}

Then, in your TestController.cs: 然后,在您的TestController.cs中:

public class TestController : Controller
{
    public ActionResult Test()
    {
        var obj = new Foo();
        //Do some processing
        return View(obj);
    }
}

In this way, you can access http://www.foo.com/test.asp without issues, and maintaining the .asp your client requires. 这样,您可以毫无问题地访问http://www.foo.com/test.asp ,并维护客户端所需的.asp

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

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