2
votes

I want the url to be

domain.com/doc/{project}

I have a controller called Projects or "ProjectsController.cs." This controller has an action (ActionResult) I want to reach with my custom url route called "DocumentationIndex."

DocumentationIndex does have a parameter. It's a simple string parameter.

The following continues to give me a 404.

routes.MapRoute(
    name: "Documentation",
    url: "doc/{project}",
    defaults: new { controller = "Projects", action = "DocumentationIndex", project = "" }
);

Can anyone point me in the right direction?

2
What is the Exception being thrown, what is the message with the exception and what is the stack trace for the exception. - Erik Philips

2 Answers

2
votes

You can use attribute routing to define this within the controller:

public class ProjectsController 
{
     [Route("doc/{*project}")]
     public ActionResult DocumentationIndex(string project = "") 
     { // ... }
}

You'll need to ensure attribute routing is enabled in your Route Config:

routes.MapMvcAttributeRoutes();

The * in the {*projects} will ensure that the route parameter matches the rest of your URL, even if it contains special characters.

0
votes

Try this:

routes.MapRoute(
    "Documentation",
    "doc/{project}",
    new { controller = "Projects", action = "DocumentationIndex" }
);

Your controller code:

public class ProjectsController ... {
    //
    public ActionResult DocumentationIndex(string project = "") {
        // ...
    }
}