Solved

Why my routes.MapRout not working ?

Posted on 2015-02-06
11
60 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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