Bruno Bruno - 1 year ago 66
C# Question

How to Deal with Unused MVC Routing Parameters

I have an MVC application and the UI team has requested that urls for a careers page follow this format:{jobTitle}/{JobCode}
. I can do that in the careers controller like this:

public ActionResult Detail(string jobTitle, string jobCode)
var model = getModelFromDb(JobCode);
return View(model);

The problem is that we look up the career info by job code not title so we end up with an unused parameter. I don't think this is best practice. Especially since you can enter whatever you want for the career title and still get the job details back. A url like this would still work:

I suppose we could always look up careers by title and code but that seems kind of pointless since the codes are unique.

Is there a better way to implement the extra parameter in the url and keep from allowing invalid job titles from being put in the url?

Answer Source

What we ended up doing is passing both parameters to the controller(jobTitle and jobCode), looking up the record by jobCode, and validating that the record's slug matches the jobTitle passed in(all case insensitive). This way we prevent bogus urls from returning job detail pages and we keep the UI team happy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download