0

I am new to web apps with asp.net, I have tried to map my own url and ran into some issues. I have the following code

//controller
 public class CuisineController : Controller
{
    //
    // GET: /Cuisine/
    public ActionResult Search()
    {

        return Content("Cuisine");
    }



//Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathinfo}");

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

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", aciton = "Index", id = UrlParameter.Optional}

            );


    }
}

I know this is extremely basic code, but everytime i run it with the url ~/cuisine/{name} I get an 404 error? Can someone please tell me why? Thanks!

1
  • Add the name parameter to the action method public ActionResult Search(string name) {.. then /Cuisine/Indian will navigate to the Search method and the name parameter will be "Indian" Commented Sep 18, 2014 at 1:07

1 Answer 1

1

Your problem is in this block of code

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

Specifically, you are passing in this line

  action = "Search",

Which makes no sense to this line here

  "Cuisine/{name}",

In order of it to work you will need to either implement the {action} parameter, or simply remove it all together.

The reason for this is because you are stating that your url will look like Cuisine/{name} which means you will pass in a parameter called name with whatever value. However in your new statement you are including another parameter called action, which isn't understood by ASP because you haven't stated that it's part of the url. So ASP will look for Cuisine/{action}/{name} and when you pass in just Cuisine/{name} it won't understand what to do.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem buddy, if my answer solved the problem please mark it as correct. Have a good one!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.