Reputation: 57
Writing with MVC in C#, I have a device that issues an HTTP request to {host}/clock
. I want different devices to write to different places, so I want to enter the host as {.../company1/}
, {.../company2/}
. I would then have a CompanyController
that would redirect requests for company1
, company2
, etc. without hardcoding the names of the companies.
So a request to {host}/company/company1/clock
would write to company1's database. Presumably if I entered {host}/company/notacompany/
, I would still want to see the value "notacompany"
, so I can redirect it somewhere else. How would I go about catching company1
as an address within the company controller without specifically coding a company1
action, or is this the wrong way to go about this?
Upvotes: 2
Views: 131
Reputation: 25351
You can use attribute routes to tell the routing engine what to do with the route parameter (company1
, company2
, etc...). You can use a switch
and handle the notacompany
in the default
:
public class CompanyController : Controller {
[Route("/[controller]/{companyId}")]
public ActionResult Index(string companyId) {
switch(companyId) {
case "company1":
//Process company1.
case "company2":
//Process company2.
default:
//Process 'notacompany' here.
}
}
}
Upvotes: 3