1

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

1 Answer 1

2

Try adding this route above your default:

routes.MapRoute(
    name: "Profile",
    url: "alumcloud/{companyName}",
    defaults: new { controller = "AlumCloudController", action = "Details" }
);
Sign up to request clarification or add additional context in comments.

5 Comments

Perfect. Though, you might want to change value of name in name: "Default" to something else. It could hurt rest of the app.
Thanks for the catch @Abhinav - missed that one.
I'm missing something somewhere. I've updated the original post with my issues. I cannot seem to get the url to read mydomain/ac/acompanyname. It keeps changing it back to mydomain/acompanyname
2 things: 1) your new route needs to go above the default. 2) In your screenshot, you're missing the /ac/ part of the URL.
Turns out that my naming convention was incorrect. I've got a folder named AlumCloud and that was what MVC routing was pointing to. So I changed the name of the controller and it works. Thank you for showing me the correct way to add the routing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.