简体   繁体   English

Visual Studio 2010 ///.NET MVC应用程序-空白项目出现404错误

[英]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. 我在vs2010中创建了一个空的mvc项目,并创建了一个视图(MessageView),一个控制器和一个模型。 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: 由于某种原因,当我构建应用程序(将视图设置为起始页)时,它似乎抛出404错误并将以下内容附加到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: 研究完web.config文件后,可以看到以下代码:

<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. 这已自动添加。...不完全确定为什么要考虑单击空的MVC项目。 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. 已解决:问题是我将MessageView移到了.NET放置在其中的Views子目录中。 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. 我还必须从web.config文件中删除上面的代码。

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. 这不是错误,而是MVC的工作方式。 Your URL should be: 您的网址应为:

http://localhost/MvcApplication1/ControllerName http:// localhost / MvcApplication1 / ControllerName

Not

http://localhost/MvcApplication1/Views/MessageView.aspx 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). MVC不允许Web服务器在/ Views(/Views/Whatever.aspx)中公开您的原始视图文件。 When you try to access it, it causes a 403 forbidden header to be returned. 当您尝试访问它时,它将导致返回403禁止的标头。

ASP.NET handles the 403 by redirecting to /Account/LogOn -- the LogOn action method of the AccountController. ASP.NET通过重定向到/ Account / LogOn(AccountController的LogOn操作方法)来处理403。 Since you have a blank project, and there is no AccountController, this is what is causing your 404. 由于您有一个空白项目,并且没有AccountController,这就是导致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. 这些是MVC使用的特殊文件夹。

If you want to access a view in a subdirectory, create a subdirectory like /content or /scripts. 如果要访问子目录中的视图,请创建一个子目录,例如/ content或/ scripts。 MVC treats these as normal folders, and you can access whatever you want from them. MVC将它们视为普通文件夹,您可以从其中访问任何内容。 So, you could put the file in a folder like this: 因此,您可以将文件放在这样的文件夹中:

/SubdirectoryAbc/MessageView.aspx. /SubdirectoryAbc/MessageView.aspx。

You should then be able to access this file using the URL http://localhost/MvcApplication1/SubdirectoryAbc/MessageView.aspx . 然后,您应该能够使用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. 问题在于,由于MVC具有路由功能,因此它对文件结构使用约定。 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. 这将禁用默认情况下使用MVC放置到站点上的身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2010中的Project WPF .Net 3.5导致错误 - Project WPF .Net 3.5 in visual studio 2010 cause error 从ASP.NET 4网站项目迁移到Visual Studio 2010中的Web应用程序项目是否导致所有控件引发错误? - Migrating from ASP.NET 4 website project to web application project in Visual Studio 2010 Causing all controls to throw error? 在Visual Studio 2010上安装MVC3时出错 - Error installing MVC3 on Visual Studio 2010 Visual Studio 2015:抑制生产中的.NET MVC应用程序中的错误? - visual studio 2015: suppress error in .net mvc application in production? 在Visual Studio 2010中从.NET项目启动C ++项目 - Launching a C++ project from a .NET project in Visual Studio 2010 在调试MVC 4项目时是否可以在Visual Studio 2010中编辑并继续? - Is it possible to Edit and Continue in Visual Studio 2010 while debugging an MVC 4 project? 本地主机上的 ASP.NET MVC Visual Studio http 404 错误,找不到资源。 - ASP.NET MVC visual studio http 404 error on local host, The resource cannot be found. 如何在Visual Studio中组织.NET MVC应用程序 - How to organize a .NET MVC application in Visual Studio Visual Studio 2010中的ASP.Net MVC 1.0 - ASP.Net MVC 1.0 in Visual Studio 2010 MVC 3,Visual Studio 2010,.Net Framework 4.0中的正则表达式验证 - Regular Expression Validation in MVC 3, Visual Studio 2010, .Net Framework 4.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM