Solved

Why my routes.MapRout not working ?

Posted on 2015-02-06
11
55 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
  • 6
  • 4
11 Comments
 
LVL 10

Expert Comment

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

Author Comment

by:Varshini S
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

by:Snarf0001
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

by:Snarf0001
Comment Utility
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
Comment Utility
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 22

Expert Comment

by:Snarf0001
Comment Utility
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
Comment Utility
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
Comment Utility
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 22

Accepted Solution

by:
Snarf0001 earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now