shujaat siddiqui
shujaat siddiqui

Reputation: 1587

URL Routing MVC 5 Asp.net

I know about routing in MVC. I added a new MapRoute under RegisterRoute method in RouteConfig.cs class and successfully called my function with the URL http://localhost:53363/package/PackageDetail/mypackage/5.

However, my question is do i have to add different Map Routes for every method or is there any better way ? Like in PackageController class you can see i have two methods one methods takes PackageId and PackageName and the other takes only PackageId. So do i have to register different Map Routes or not ?

RouteConfig

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

            routes.MapRoute(
            name: "Package",
            url: "Package/PackageDetail/{packageName}/{packageId}",
            defaults: new { controller = "Package", action = "PackageDetail", packageName = UrlParameter.Optional, packageId = UrlParameter.Optional }

        );

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

PackageController.cs :

        [HttpGet]
        public ActionResult PackageListing(int packageId = 0)
        {

            return View();
        }

        [HttpGet]
        public ActionResult PackageDetail(string packageName = "", int packageId = 0)
        {

            return View();
        }

Upvotes: 0

Views: 545

Answers (2)

krillgar
krillgar

Reputation: 12815

Despite the fact that Muhammed's answer will work, it is very repetitive, especially if you're using the same style of routes for multiple types.

There are a few things to consider before deciding upon a single approach to routing. The main one is why have both the name and ID in the route? If you want a more SEO friendly URL structure, don't bother with the ID at all.

If you have multiple products within the same type that have identical names, then there's no point in including the name as part of the URL since that won't get a user where they want to go by itself. In that event, just leave the original route.

However, if you have several different controllers (or actions) with a similar name/id structure for the routes, you'll be far better served with making your custom route more generic.

routes.MapRoute(
    name: "NameAndId",
    url: "{controller}/{action}/{name}/{id:int}",
    defaults: new 
        { 
            controller = "Package", 
            action = "PackageDetail", 
            name = UrlParameter.Optional, 
            id = UrlParameter.Optional 
        });

Keep this above the default route, and this will redirect not just

/Package/PackageDetail/Deluxe/5

but also allow you to have stuff like this:

/Meals/Menu/Dinner/3

That may not necessarily be applicable for you in this project, but since you're learning MVC, this is a good skill to pick up. The more generic you're able to maintain your route definitions, the less you'll need to repeat it. Of course, if this is a one-time special route, there's nothing wrong with using the attributes.

Also to answer your final question, you do not need to create another custom route, because your PackageListing method will be routed through the default route that was provided when you created your project.

Upvotes: 1

jignesh
jignesh

Reputation: 1669

If you want to override default route url and generate custom url then you need to register route in route config file.

You can pass Package name and package Id as below.

http://sitename/Package/PackageListing?packageId=1

http://sitename/Package/PackageDetail?packageName=packagename&packageId=1

but if you want to generate URL as below than you need to add route in route.config file.

  http://sitename/Package/PackageListing/1 

 http://sitename/Package/PackageDetail/packageName/1 

Upvotes: 0

Related Questions