[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 64
  • Last Modified:

Why my routes.MapRout not working ?

I have added cuisine controller  in my  RouteConfig.cs. But when i use this URL http://localhost:52651/cuisine
It is throwing an error HTTP 404 The resource cannot be found error. Why ?

RouteConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

            routes.MapRoute("Cuisine", "cuisine/{name}", new { controller = "Cuisine", action = "Search" });

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

Open in new window




CuisineController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OdeToFood.Controllers
{
    public class CuisineController : Controller
    {
        //
        // GET: /Cuisine/

        public ActionResult Search()
        {
            return Content("You have reached cuisine Controller");
        }

    }
}

Open in new window

0
Varshini S
Asked:
Varshini S
  • 6
  • 4
1 Solution
 
Najam UddinCommented:
routes.MapRoute("Cuisine", "{controller}/{name}", new { controller = "Cuisine", action = "Search" });
0
 
Varshini SAuthor Commented:
Najam Uddin: I have added this line

routes.MapRoute("Cuisine", "{controller}/{name}", new { controller = "Cuisine", action = "Search" });

but it is now working. And my default home controller also not working now.
0
 
Varshini SAuthor Commented:
sorry:

Najam Uddin: I have added this line

routes.MapRoute("Cuisine", "{controller}/{name}", new { controller = "Cuisine", action = "Search" });

but it is not working. And my default home controller also not working now.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
Snarf0001Commented:
Why did you use {name} instead of {action}?  Action is the standard, if you change it accordingly it will work.

routes.MapRoute("Cuisine", "cuisine/{action}", new { controller = "Cuisine", action = "Search" });

Open in new window

0
 
Varshini SAuthor Commented:
Now it is working http://localhost:52651/cuisine
But if add action it is not working   http://localhost:52651/cuisine/french

it showing error:

The resource cannot be found.
  Description: HTTP 404. The
0
 
Snarf0001Commented:
Okay, I think I see the intent.  You want "French" to be recognized as the name you're searching for?  Which is why you called it name originally?

If so, you can implement two custom handlers, one for just "cuisine" and one for the name like you had it:
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("Cuisine", "cuisine", new { controller = "Cuisine", action = "Search" });
            routes.MapRoute("CuisineResults", "cuisine/{name}", new { controller = "Cuisine", action = "Search" });

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

Open in new window


Then have the controller set up like so:
        public ActionResult Search(string name)
        {
            return Content(string.Format("You have reached cuisine Controller - {0}", name));
        }

Open in new window



But note that this will break if you try to add any actions other than search to the controller unless you custom route each one of them.

If you're trying to do something more complex, would suggest either going with the standard convention, or look at implementing a custom route handler.
0
 
Varshini SAuthor Commented:
My intention was to add own route. So I added the route in front of the default route in RouteConfig file.  The controller name should be Cuisine and the default action should be search.

routes.MapRoute("Cuisine", "cuisine/{name}", new { controller = "Cuisine", action = "Search" });

 If you have a request come in for a /cuisine/French  that should go to the cuisine controller and pass along name value as French.
If you have a request come in for /home/index that is not going to match the cuisine route  instead routing engine evaluate the next route , which is the default route it will match there.

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

cuisineController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OdeToFood.Controllers
{
    public class CuisineController : Controller
    {
        //
        // GET: /Cuisine/

        public ActionResult Search(string name)
        {
            name = Server.HtmlEncode(name);
            return Content(name);
        }

    }
}

Open in new window


routeConfig

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

           routes.MapRoute("Cuisine", "cuisine/{name}", new { controller = "Cuisine", action = "Search" });

            //routes.MapRoute("Cuisine", "cuisine/{action}", new { controller = "Cuisine", action = "Search"});

            //routes.MapRoute("Cuisine", "{controller}/{name}", new { controller = "Cuisine", action = "Search" }); 

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

Open in new window

0
 
Snarf0001Commented:
So are you still having problems?  Please reread the code in my last post.
This will work as long as you put in both of the custom routes.

Since you're not using the action attribute, you need to have one for just "cuisine" and one for "cuisine/{name}".

Let me know if you're still having problems.
0
 
Varshini SAuthor Commented:
You mean If I use the action attribute there is no need add another one. Am I correct ?
Please let me know how do I add action attribute to this.
0
 
Varshini SAuthor Commented:
Snaff0001: It is working fine when I add the second line. But I don't know why I need to add a another line.
But look at the default controller it has one line. But for cuisine controller why need two lines ? Please explain

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

Open in new window


cuisineController

  routes.MapRoute("Cuisine", "cuisine", new { controller = "Cuisine", action = "Search" });
            routes.MapRoute("CuisineResults", "cuisine/{name}", new { controller = "Cuisine", action = "Search" });

Open in new window

0
 
Snarf0001Commented:
Hey Varshini S, sorry for neglecting this one.
Basically, it's because MVC by default is expecting controller/action/id.  The default routing is set up to handle it this way.

The first route in there is for just "cuisine", where you're overriding the default "action" of index, to search.  Which works fine.
But then when you pass in "cuisine/[somethingelse]", the standard MVC routing is interpreting [somethinglese] as the action, which doesn't exist in your controller.

You could eliminate all of the custom routes if it's possible to just use the urls as "cuisine/search/french", but I realize clients don't always appreciate making it easy for the developer.

If it's becoming a pain, you can look at custom route handlers:
https://msdn.microsoft.com/en-us/library/system.web.mvc.mvcroutehandler(v=vs.118).aspx

Which basically lets you override an entire section with whatever parameters / order / sequence / names you need.
I think it's overkill for what you're trying to accomplish, but will probably come in handy in the future.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now