Using MVC 5.2, VS 2013.
If I want a custom route on my site like, "www.mydomain.com/Electronics" that a end visitor can hit where Electronics is a category parameter that could really be any available category value and hits a
class method within a Category controller? How do I set this up in the Category controller with attribute routing? And how do I call it with either Url.RouteUrl or Url.HttpRouteUrl? Is there a way to just use the attribute routing to achieve this and
not have to place an entry into RouteConfig.cs?
Thanks,
Ian
I actually solved this problem with attribute routing on a Category Controller that I created. So now you can enter any category description as a parameter off the root of the site, e.g.
http://www.mywebsite.com/Electronics as an example, as long as it has a min length of 1 byte.
public class CategoryController : Controller { public class CategoryController : Controller { private MyContext db = new MyContext(); public ActionResult Index() { return View(); } [Route("{CategoryDesc:minlength(1)}",Name="Category")] public ActionResult DisplayCategory(String CategoryDesc) { // validate the incoming category description and do your stuff...
return View("Index"); } } }
I can also refer to it in an anchor tag like this:
<a href="@Url.RouteUrl("Category", new { CategoryDesc = "Electronics" })">