我在 MVC 3 中有一个项目,我想为每个客户提供一个特定的 url。
样本:
www.mysite.com/CustomerOne
www.mysite.com/CustomerTwo
.
我已经注册了所有路线并且效果很好。
问题是:我必须在所有操作中都期望客户名称的第一个参数。
我想要类似的东西,在自定义 Controller 上有一个属性,告诉我那里有什么客户。
代码:
routes.MapRoute(
"PerCustomer", // Route name
"{customer}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", customer = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
);
public class HomeController : Controller
{
public ActionResult Index(string customer)
{
//do stuff
return View();
}
public ActionResult SaveSomething(string customer, string param1, ...)
{
//save stuff for the customer
return View();
}
}
谢谢..
最佳答案
像这样创建一个 BaseController 类:
public class BaseController : Controller
{
public string CurrentCustomer
{
get
{
return (string)RouteData.Values["customer"];
}
}
}
在你的 Controller 中:
public class HomeController : BaseController
{
public ActionResult Index()
{
//do stuff
DoSomethinwWith(this.CurrentCustomer);
return View();
}
public ActionResult SaveSomething(string param1, ...)
{
DoSomethinwWith(this.CurrentCustomer);
return View();
}
}
关于asp.net-mvc - 每个客户的 Asp MVC 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8325793/