-1

我想了解如何设置路由参数以执行以下操作:当用户调用像http://hostname.com/shortenedurl这样的缩短网址时,我的 asp.net mvc 项目应该调用像http://hostname.com这样的操作和参数/controller/action
我以前从来没有做过这样的事情,所以我将不胜感激任何建议。我已经知道如何编码和解码 url 字符串从正常到短和返回。

4

2 回答 2

1

RouteConfig.cs您可以在文件中定义特定的路由。例如,.../MyShortUrl将路由到Edit方法EmployeeController

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

    routes.MapRoute(
      name: "MyShortUrl",
      url: "MyShortUrl",
      defaults: new { controller = "Employee", action = "Edit", id = UrlParameter.Optional }
    );

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

注意特定路线需要在更一般路线之前按顺序排列

于 2015-01-30T05:11:59.563 回答
0

没有“正常”和“短”。你应该使用路由。最好的办法是看一些关于路由的东西,以了解这一切是如何工作的。

于 2015-01-30T04:50:33.467 回答