开发者

Adding new pages to MVC 3 project results in "Resource not found" error

开发者 https://www.devze.com 2023-04-01 03:44 出处:网络
I have an existing MVC 3 project I\'ve been working on for a while. Today I decided to add a new page. I added a new view, hooked it up with an actionresult on an existing controller but no dice. I ge

I have an existing MVC 3 project I've been working on for a while. Today I decided to add a new page. I added a new view, hooked it up with an actionresult on an existing controller but no dice. I get a 404. I thought maybe something was wonky with the view so I did a response.write in the controller (to take the view out of the equation). No dice. I tried creating a new controller. Same thing. I tried moving an existing page. Same thing. All existing pages work, but I can't add new ones or move existing ones. My keyboard is covered in hair. Please help.

My global.asax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

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

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

        routes.MapRoute(
            "Home",
            "",
            new { controller = "Home", action = "Index" }
        ); 
    }

    protected void Application_Star开发者_C百科t()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

The view I'm trying to add is /Views/Restaurants/Nearby.

The controller is /Controllers/RestaurantsController and the ActionResult is:

public ActionResult Nearby()
    {
        return View();
    }


Start by removing the following useless line from your route definitions:

routes.MapRoute(
    "Home",
    "",
    new { controller = "Home", action = "Index" }
);

so that you only have the default route setup:

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

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

Now in order to access your new controller action you would navigate to /Restaurants/Nearby. This will render the Nearby action on the Restaurants controller:

public class RestaurantsController: Controller
{
    public ActionResult Nearby()
    {
        return View();
    }
}

which obviously would execute the ~/Views/Restaurants/Nearby.cshtml view.


I know this is an old thread but I have found many many 'Resource Not Found' solutions which did not help me. After some time I worked out my issue. Hopefully it will help someone else.

The problem was that I had been messing about with deploying my application, so my project happened to be set to a 'Release' configuration. I had also altered the release configuration to compile to a different folder.

So I think when I was hitting 'Run' in the toolbar, it was picking up an older DLL and running from that.

I only spent three hours on this so I'm happy.


You could try adding the Route Debugger NuGet package IIRC,

Get-Package routedebugger

If needed, change your web.config entry to activate the route debugger for all requests:

<add key="RouteDebugger:Enabled" value="true" />

Then you should build and run your project, and navigate to /Restaurants/Nearby/. The resulting diagnostics should tell you where your routing issue (if any) lies. If you are still having trouble, edit your post with the route debugging info and we'll see what we can do!

0

精彩评论

暂无评论...
验证码 换一张
取 消