0

I am creating a asp.net mvc 4 application

public class AspNetController : Controller
{
    //
    // GET: /AspNet/
    public ActionResult Index()
    {
        return View();
    }

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

as Shown Above There is AspNet Controller and Introduction Action Method

Default Url for Introduction Action Method is

localhost:55134/aspnet/introduction

But I Want Url Like

localhost:55134/aspnet/introduction-To-AspNet

Same for /localhost:55134/aspnet/NetFrameWork To

/localhost:55134/aspnet/What-is-.Net-Framework

How to do that

3 Answers 3

3

You should be able to use the ActionName attribute to decorate your routes.

[ActionName("Introduction-To-AspNet")]
public ActionResult Introduction()
{
    return View();
}

You really want to use AttributeRouting, either via a 3rd party package or natively if you can.

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

1 Comment

Thanks a lot. This is the solution i was looking for , i have to do just one modification for this to work is return View("Introduction");
1

Technically this concept comes under Routing in ASP.NET MVC.

For this you need to do an entry for route in App_Start->RouteConfig.cs file under RegisterRoutes(RouteCollection routes)

For Example:

routes.MapRoute(
    "customRouteName", 
    "aspnet/introduction-To-AspNet", 
    new { controller = "AspNet", action = "Introduction" });

here aspnet/introduction-To-AspNet will append after your base url i.e. localhost:55134/

Comments

1

The quick and dirty answer is to add a route to your ~/AppStart/RouteConfig.cs file and it will be taken care of:

routes.MapRoute(
    name: "CustomRoute",
    url: "Aspnet/Introduction-To-AspNet",
    defaults: new { controller = "Home", action = "AspNet", id = UrlParameter.Optional }
);

However, I'm assuming this is for some type of blog? I would reccomend that you have an action method called view, and then use your name as a parameter for the article. That way, you don't have to go in and edit the code every time you add a new article or other content:

public class ArticlesController : Controller
{
    public ActionResult ViewArticle(string? title)
    {
        ViewBag.Article = title;
        return View();
    }
}

that way, your URL would be www.yoursite.com/Articles/ViewArticle/Introduction-To-AspNet. In general, you don't want to add tons of specific routes to your route config if you can avoid it. That being said, if this is a legacy system, the route table may be the only way.

EDIT

Ok, so what you can do is pass the string into the ViewBag and use a case statement to determine which partial view to show (I think this just might be your ideal solution):

<!--cshtml view-->
@switch(ViewBag.Article)
{
    case 'Introduction-To-AspNet':
        @Html.Partial('pathToPartialView.cshtml')
        break;
    case 'Some-Other-Article'
        @Html.Partial('pathToAnotherPartialView.cshtml')
        break;
    ...
    ...
    default:
        @Html.Partial('invalidArticleName.cshtml')
        break;
}

The controller will pass the article name through the ViewBagand then you can use the case statement to figure out which article to render... and of course, the real secret sauce you've been looking for: @Html.Partial('URL') - this will take your partial and render it right were you put that in the page. You can also pass objects to that just as an FYI.

In addition, make sure that you have a default action on the switch statement that will show some sort of 404 page that indicates that the name in the URL was invalid. You ALWAYS want to have this anytime you're taking user input from the URL because people monkey with URLs all the time (and more innocently, copy+paste them wrong/incompletely all the time)

2 Comments

Thanks I want a solution like your second suggestion. But I just have action methods and views. And nothing to dour with database. How to do that
Thanks , But this is the solution i was looking for is [ActionName("Introduction-To-AspNet")] public ActionResult Introduction() { return View("Introduction"); }

Your Answer

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