How to customize MVC url path

I am trying to create a clean URL path to an MVC controller and I need a little assistance.

Example desired path: http://www.linkedin.com/in/alumcloud. This path is from LinkedIn and notice how it has the /in/alumcloud.

I would like for mine to read: http://www.alumcloud.com/alumcloud/somecompanyname

How would I do this with an MVC controller?

Below is the only code in my MVC controller, becuase this controller is only needed to respond to GET HTTP methods.

public class AlumCloudController : AlumCloudMvcControllerBase
{
    // GET: /AlumCloud/Details/5
    public ActionResult Details(string companyName)
    {
        return View();
    }
}

--Here is my RouteConfig

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {

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

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

-----------------------------------2nd Attempt------------------------------------

--Route Code 2

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

            routes.MapRoute(
                name: "Public",
                url: "ac/{companyName}",
                defaults: new { controller = "AlumCloudController", action = "UserProfile" }
            );
 }

--Controller Code 2

public class AlumCloudController : AlumCloudMvcControllerBase
{
    // GET: /AlumCloud/Details/5
    public ActionResult UserProfile(string companyName)
    {
        return View();
    }
}

--The URL

'/ac/' + options.person.CompanyName 

--Snap shot of 404 error

404 mvc error

Upvotes: 1

Views: 2054

Answers (1)

Matt Millican
Matt Millican

Reputation: 4054

Try adding this route above your default:

routes.MapRoute(
    name: "Profile",
    url: "alumcloud/{companyName}",
    defaults: new { controller = "AlumCloudController", action = "Details" }
);

Upvotes: 2

Related Questions