简体   繁体   中英

Visual Studio 2010 //.NET MVC Application - 404 Error on blank project

I created an empty mvc project in vs2010 and created a view (MessageView), a controller and a model. For some reason when I build my application (having set my view as the start page), it seems to throw a 404 error and appends the following to the URL:

http://localhost/MvcApplication1/**Account/LogOn?ReturnUrl=%2fMvcApplication1%2fViews%2fMessageView.aspx**

Having investigated my web.config file I can see the following piece of code:

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>

This has been added automatically....Not entirely sure why considering I clicked empty MVC project. Anyone know the cleanest way of fixing this....

EDIT

SOLVED: The issue is that I moved MessageView to the Views subdirectory placed there by .NET. I had to place the view at the root of the project directory in order for it to be able to locate and display it. I also had to remove the code above from the web.config file.

How can you get this to work when it is placed within a subdirectory

只需将Web选项卡上项目属性中的Start Action值从默认值Current Page更改为其他值即可。

You do not want to access the view, you want to access the controller. This is not a bug, it is how MVC works. Your URL should be:

http://localhost/MvcApplication1/ControllerName

Not

http://localhost/MvcApplication1/Views/MessageView.aspx

Update

Here is what is happening. You started with a blank project. This means you do not have an Account controller.

MVC does not allow the web server to expose your raw view files in /Views (/Views/Whatever.aspx). When you try to access it, it causes a 403 forbidden header to be returned.

ASP.NET handles the 403 by redirecting to /Account/LogOn -- the LogOn action method of the AccountController. Since you have a blank project, and there is no AccountController, this is what is causing your 404.

Another Update

Just read your SOLVED. Again, you cannot access files in the Views, Controllers, or Models folders. These are special folders used by MVC.

If you want to access a view in a subdirectory, create a subdirectory like /content or /scripts. MVC treats these as normal folders, and you can access whatever you want from them. So, you could put the file in a folder like this:

/SubdirectoryAbc/MessageView.aspx.

You should then be able to access this file using the URL http://localhost/MvcApplication1/SubdirectoryAbc/MessageView.aspx .

However, you would not be able to use the view in a controller action method.

The issue is that MVC uses convention for file structure due to its routing. If you are to move the views to another folder, you would need to customize the routing code to to map properly to your folder structure. Honestly though, I don't think it is worth your time.

You can just remove this whole section;

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>

This will disable the authentication that has been put onto the site by default with MVC.

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