1
如何創建URL,如www.company.com/clientNameA,www.company.com/clientNameB,以便我可以使用clientNameA或clientNameB並在MVC中調用操作?創建自定義URL以在MVC中進行自定義搜索
如何創建URL,如www.company.com/clientNameA,www.company.com/clientNameB,以便我可以使用clientNameA或clientNameB並在MVC中調用操作?創建自定義URL以在MVC中進行自定義搜索
在你的路由配置,你可以試試:
如前所述OP一直在尋找下路由表:
routes.MapRoute(
name: "Brand",
url: "{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
});
替代使用單獨的控制器/動作:
路線表
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "",
defaults: new { controller = "Home", action = "Index"
});
routes.MapRoute(
name: "Foo",
url: "{client}",
defaults: new {
controller = "Foo",
action = "YourAction",
client = UrlParameter.Optional
});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
,並添加控制器:
控制器
public class FooController : Controller
{
//
// GET: /Foo/
public ActionResult YourAction(string client)
{
return null;
}
}
感謝您好,我有默認路由 「URL:」{控制器}/{行動}/{ID }「」這不會衝突? – Patrick
是的,但似乎你想打破{控制器}/{動作}邏輯。您可以爲特定控制器指定它以這種方式工作:創建專用控制器並將路由添加到默認路由上(路由映射條目依賴於它們添加的順序) – Stefan
而我該如何構建操作?索引(字符串ID)?我得到資源找不到。 – Patrick