开发者

ASP.NET MVC Routes and Client Specifc Functions

开发者 https://www.devze.com 2023-03-08 12:19 出处:网络
I\'m working on an application where a system is being built where multiple \"customers\" will use the system and 99.9% of the controllers/actions will be the same, just pulling different data, but th

I'm working on an application where a system is being built where multiple "customers" will use the system and 99.9% of the controllers/actions will be the same, just pulling different data, but there are times and places where a custom controller action, or view might be needed.

Right now I'm using a default route similar to the following to get the company name with requests.

routes.MapRoute(
    "Default", // Route name
    "{company}/{controller}/{action}/{id}",
    new {company = "Unknown", controller = "Home", 
    action = "Index", id = UrlParameter.Optional} 
    );

This works great as I can have my individual controller actions defined like this

public ActionResult ShowReport(string company)
{
    //Actual code goes here..
}

I have a system in place that will get the data segment for this specific company and return the proper view. SO for my 99.9% situation thi开发者_高级运维s looks great. What I'm looking for is a solution for when I need to render a different view, or have additional actions that are specific to one company.

I could add in switch or other logic within my action, but that feels overly dirty...


For a specific company you can use something like this and put it before the default action, in this case url has to contain Company1/somethingcontroller/etc/etc.

routes.MapRoute(
    "Company1Default", // Route name
    "Company1/{controller}/{action}/{id}",
    new {company = "Company1", controller = "DefaultControllerForCompany1", 
    action = "Index", id = UrlParameter.Optional} 
    );


While I actually lean to Jay's answer pertaining to the use of data within models, I think that there is another option. Be warned that I haven't played this all the way out and don't have a full understanding of your application...

Why would you want to hardcode a company name within your global.asax? I don't think that it would be very scalable. If you want to add support for an additional 10 companies, you'd have to create 10 new entries. Also, what if you want to change the name of a company because of a buyout or something? More maintenance.

Why not add a route to send every company to the same controller like...

routes.MapRoute(  
    "CompanyRouting", // Route name  
    "{companyname}/{action}",  
    new { controller = "MySingleCompanyControllerName", action = "Index", companyname = UrlParameter.Optional }  
);

MySingleCompanyController.cs
Once in your controller you can just get whatever the companyname value is whenever you want it.

public ActionResult Index()
{
    ViewData["companynamevalue"] = RouteData.Values["companyname"];
    return View();
}

Index.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Index</title>
</head>
<body>
    <div>
        Requested Company Name = <%: ViewData["companynamevalue"] %>
    </div>
</body>
</html>

NOTE: One more thing to look into for routing help is Phil Haack's routing debugger.


Defining which view is return from an action is easy:

return View("Index");

This will return the view named "Index" no matter which action is invoked.

On the other hand, both views and actions must be defined at compile time, so you cannot dynamically create them (from what i know of).

I might suggest implementing the Command Pattern in your action to get exactly what you would want for individual companies from a single action.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号