简体   繁体   中英

MVC areas and “404 resource not found” error

I've inherited a broken MVC app from a programmer who left the company before I joined. The previous programmer, in his last act here, reorganized the app into Areas (as shown in the attached Solution Explorer snapshot). I'm pulling my hair out over what I am sure is a simple thing, but as I am new to MVC it is, at the moment, a huge thing:

Many of the links generated by the markup @Html.ActionLink() are working properly, but some are failing with a "404 resource cannot be found [...]" error.

Here is an example of a link that fails:

@Html.ActionLink("Upload Documents", "BlankPageTest", new { controller = "DocumentUpload", area = "Documents" }, new { @class = "btn btn-info" })

... and here is a link that works:

@Html.ActionLink("Edit Questions", "ChangeQuestions", new { controller = "Account", area = "UserAccount" }, new { @class = "btn btn-info" })

Both of these links are in the markup for the View "MyAccount.cshtml", which lives in the area "UserAccount". The only functional difference I can see between the link that fails and the one that works is that resource for the working link (ChangeQuestions.cshtml) LIVES IN THE SAME AREA as the view (MyAccount.cshtml). However, since both the controller and area are parameters for Html.ActionLink(), I don't see how MVC would be getting confused.

The full URL given in the "404 resource not found" message is:

/CROMERR.Website/Documents/DocumentUpload/BlankPageTest

I have a method in the controller for the targeted URL named "DocumentUpload"... cannot find any more obvious things to try so thought I'd ask someone more knowledgeable. Any and all help appreciated.

在此处输入图片说明

DocumentAreaRegistration contains the code:

public override string AreaName 
{ 
    get 
    {
        return "Documents"; 
    } 

} 

public override void RegisterArea(AreaRegistrationContext context)
{ 
    context.MapRoute( 
        "Documents_default", 
        "Documents/{controller}/{action}/{id}", 
        new { action = "Index", id = UrlParameter.Optional });
}

Coming from a previous question that you asked, your issue is having your view in the wrong location which is causing the 404 error.

From the DocumentUpload controller, you are trying to return a view BlankTestPage.cshtml . However, when viewing your screenshots, the BlankTestPage.cshtml view is actually located in the Views/Documents folder. By default, MVC will only look in the folders Views/ControllerName (where controller name is the name of the controller trying to return the view) and Views/Shared .

If you need to reference BlankTestPage.cshtml from multiple controllers, place it in the Shared folder. Else, you need to move that view from the Documents View folder into the DocumentUpload view folder (the empty one in your screenshot).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM