0

I have a Controller named About and an Action named Index. I want the URL to be like this (action name will be dynamically):

www.example.com/about/aaa
www.example.com/about/bbb
www.example.com/about/ccc

Routing

routes.MapRoute(
    name: "About",
    url: "{controller}/{name}",
    defaults: new { controller = "About", action = "Index"}

Controller

public class AboutController : Controller
{
    // GET: /About/
    public ActionResult Index(string name)
    {
        return View();
    }
}

View

@{
    ViewBag.Title = "Index";
}

<h2>Index About</h2>

3 Answers 3

4

This should work.

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

Make sure your default route exists and comes after About route

Sign up to request clarification or add additional context in comments.

3 Comments

Its not working. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /About/ip
@Velu make sure your default route exists and comes after About route.
Thanks its working now. Previously default route is first and then i have About route. After changing the position its working fine.
0
routes.MapRoute(
    name: "About",
    url: "about/{name}/{id}",
    defaults: new { controller = "About", action = "Index", id=UrlParameter.Optional}

1 Comment

Its not working. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /About/ip
0

You can pass an ActionResult name as a parameter:

public ActionResult Index(string name)
{
    return View(name);
}    

public ActionResult First()
{
    return View();
}

public ActionResult Second()
{
    return View();
}

In the View:

@Html.ActionLink("Get Action named Firts" "Index", "Home", new {name = "First"}, null)

Comments

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.