Question

How can I create URL's like www.company.com/clientNameA, www.company.com/clientNameB so I can take the clientNameA or clientNameB and invoke a action in MVC?

Was it helpful?

Solution

In your routing configuration, try something like:

As stated OP was looking for the following routing table:

routes.MapRoute( 
          name: "Brand", 
          url: "{id}", 
          defaults: new { 
             controller = "Home", 
             action = "Index", 
             id = UrlParameter.Optional });

routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { 
             controller = "Home", 
             action = "Index", 
             id = UrlParameter.Optional 
          });

Alternative with separate controller/actions:

The route table

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
          name: "Home",
          url: "",
          defaults: new { controller = "Home", action = "Index"
       });

routes.MapRoute(
          name: "Foo",
          url: "{client}",
          defaults: new { 
             controller = "Foo", 
             action = "YourAction", 
             client = UrlParameter.Optional 
          });

routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { 
             controller = "Home", 
             action = "Index", 
             id = UrlParameter.Optional 
          });

and add a controller:

The controller

public class FooController : Controller
{
    //
    // GET: /Foo/
    public ActionResult YourAction(string client)
    {
        return null;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top