Varshini S
asked on
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
CuisineController.cs
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 }
);
}
}
}
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");
}
}
}
routes.MapRoute("Cuisine", "{controller}/{name}", new { controller = "Cuisine", action = "Search" });
ASKER
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.
routes.MapRoute("Cuisine",
but it is now working. And my default home controller also not working now.
ASKER
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.
Najam Uddin: I have added this line
routes.MapRoute("Cuisine",
but it is not working. And my default home controller also not working now.
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" });
ASKER
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
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
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:
Then have the controller set up like so:
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.
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 }
);
Then have the controller set up like so:
public ActionResult Search(string name)
{
return Content(string.Format("You have reached cuisine Controller - {0}", name));
}
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.
ASKER
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
routeConfig
routes.MapRoute("Cuisine",
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);
}
}
}
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 }
);
}
}
}
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.
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.
ASKER
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.
Please let me know how do I add action attribute to this.
ASKER
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
cuisineController
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 }
cuisineController
routes.MapRoute("Cuisine", "cuisine", new { controller = "Cuisine", action = "Search" });
routes.MapRoute("CuisineResults", "cuisine/{name}", new { controller = "Cuisine", action = "Search" });
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.