0

我的应用程序需要能够让用户使用 foo.com/pubid 约定导航到页面,其中 pubid 是随机生成的字符串,例如“wgwrfwrcwrf”。

我的Home控制器定义了一个 Index ActionResult:

    public ActionResult Index(string id)
    {
         //do stuff
    }

当我在我的网站上导航到此操作而不指定 ID 时,一切都很好。我返回与 home 关联的视图。这就是 bit.ly 所做的。我正在尝试在我的应用程序中翻译该 ID。

但是,如果我请求诸如 foo.com/wgwrfwrcwrf 之类的 URL,我会得到 404。我需要能够捕获此字符串以将其发送到正确的视图。这需要自定义路线吗?

4

2 回答 2

0
routes.MapRoute(
    "id", // Route name
    "{id}", // id route
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
);
于 2013-02-05T18:45:21.350 回答
0

你应该写一个像这样的包罗万象的路线......

    routes.MapRoute("catchall", "{*id}", 
                               new {
                                      controller = "Home", 
                                      action = "Index", 
                                      id = UrlParameter.Optional
                                   });

享受 :)

于 2014-04-05T10:23:09.533 回答