?
Solved

Why my routes.MapRout not working ?

Posted on 2015-02-06
11
Medium Priority
?
62 Views
Last Modified: 2015-02-13
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
Comment
Question by:Varshini S
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 10

Expert Comment

by:Najam Uddin
ID: 40594195
routes.MapRoute("Cuisine", "{controller}/{name}", new { controller = "Cuisine", action = "Search" });
0
 

Author Comment

by:Varshini S
ID: 40594207
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
 

Author Comment

by:Varshini S
ID: 40594211
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Snarf0001
ID: 40594279
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
 

Author Comment

by:Varshini S
ID: 40594437
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
 
LVL 23

Expert Comment

by:Snarf0001
ID: 40594537
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
 

Author Comment

by:Varshini S
ID: 40594901
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
 
LVL 23

Expert Comment

by:Snarf0001
ID: 40595078
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
 

Author Comment

by:Varshini S
ID: 40595186
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
 

Author Comment

by:Varshini S
ID: 40595589
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
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 2000 total points
ID: 40604742
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question