Ashwini Nayak
Ashwini Nayak

Reputation: 17

Http 404 Not Found error

I have created a number of controllers and each of them returns a number of views.

The views get displayed successfully on the browser upon entering the url, for example: /localhost:21419/Accounting/Index

There is no problem with displaying views for any of the controller views except one(named OrderController)

For this OrderController, I get a 404 error when I enter the url on the browser. for example: /localhost:21419/Order/Index.

The only time I get a page display is when I enter /localhost:21419/Order (the Index page gets displayed). But when I enter /localhost:21419/Order/Index, I get the same error. I tried creating a new action method and a view corresponding to it, but none of the views under this controller are getting rendered. What might be the problem?

My Global.asax.cs :

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        RouteTable.Routes.MapHubs();
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
} 

My RouteConfig.cs (I added namespaces as well)

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    namespaces: new[] {"RRRPropPres.WebUI.Controllers"}
    );
}

What might be the reason it doesn't work for one controller alone?

I tried

return View();

and

return View("name of View");

Nothing seems to help.

Upvotes: 0

Views: 4232

Answers (2)

Manoochehr Dadashi
Manoochehr Dadashi

Reputation: 726

It's all about your route config.

This is a basic default route :

routes.MapRoute(
            "SomeName", // Route name
            "/{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

I suggest to remove namespaces: new[] {"RRRPropPres.WebUI.Controllers"}.

also you it's better to define defaults like this: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

if id is optional and you don't send it in some requests id = UrlParameter.Optional is necessary.

if you are working with Areas, it's better to disable UseNamespaceFallback like this :

routes.MapRoute(
        ...
    ).DataTokens["UseNamespaceFallback"] = false;

Upvotes: 1

staticvoidmain
staticvoidmain

Reputation: 793

If the route works fine for other controllers, it will not be a problem with routes. You are getting a 404 and I doubt you do not have Index view under Order section. Click on the views->Order and see if Index is present. If no, we have to create one or we need to go further with your controller code. I could help further if you paste your controller code. The code snippets you pasted look all right. Right clicking on the controller and creating views might have placed your view in a different folder.

Upvotes: 0

Related Questions