asp.net-mvc - 每个客户的 Asp MVC 路由

标签 asp.net-mvc asp.net-mvc-3

我在 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/

相关文章:

c# - Kendo UI MVC 网格选择更新单独的控件

asp.net-mvc - ASPNet.Core 1.0 RTM Kendo Grid不显示数据

jquery - ASP.NET MVC 3 JQuery 验证重置单个字段

c# - 从插件加载 View 而不将其作为资源嵌入到 nopCommerce 中

javascript - 模型绑定(bind)到自定义组件 - Asp.net Mvc

c# - Site.Master 中的一些东西可以有条件地显示(MVC)吗?

asp.net-mvc - 如何使 web api 中需要 int[] 参数?

.net - 是否有比 ASP.NET 成员(member)提供程序更现代的成员(member)/安全实现

c# - 重定向返回 "Object moved to"

.net - 如何使用Razor语法在@ Html.TextBoxFor中设置值?