Search code examples
c#asp.net-mvc-3http-postvisual-studio-2012iis-express

Fresh Windows + VS2012 = MVC3 Problems


I just recently got a fresh install of Windows 7 on a my PC running VS2012/VS2010. I have an MVC3 project that ran just fine before I pulled it onto this PC to run. The project still compiles on this PC and I can navigate through my site while running the app in studio (2010 or 2012), but when I tried to POST from ANY form in ANY view and pass an ID by means of the URL like this:

    <form id="ScriptForm" action="/MyApp/ControllerName/ActionName/@ViewBag.IDNumber" method="post">
    ...
    </form>

...I got this error. I did some digging and playing around to try to fix this, ultimately taking these steps to try to resolve:

  1. Actually installed ASP.NET via the Windows Components form in Control Panel
  2. Changed the application to use IIS Express

Well, when I changed it to run under IIS Express, I started getting a different error.

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is 
temporarily unavailable.

Most likely causes:
The directory or file specified does not exist on the Web server.
The URL contains a typographical error.
A custom filter or module, such as URLScan, restricts access to the file.

Things you can try:
Create the content on the Web server.
Review the browser URL.
Check the failed request tracing log and see which module is calling SetStatus. For more information, click here.

Detailed Error Information:
Module     IIS Web Core
Notification       MapRequestHandler
Handler    StaticFile
Error Code     0x80070002
Requested URL      http://localhost:51596/MyApp/ControllerName/ActionName/1
Physical Path      C:\CODE\MyApp\ControllerName\ActionName\1
Logon Method       Anonymous
Logon User     Anonymous
Request Tracing Directory      C:\Users\cbarlow\Documents\IISExpress\TraceLogFiles\MYAPP

More Information:
This error means that the file or directory does not exist on the server. Create the file or directory and try the request again.
View more information »

It's almost as if it is not recognizing that this is a route and is trying to resolve the URL to a physical file (like 1.html) which obviously does not exist. But why isn't it "doing the MVC thing" and using the route? I have this in my global.asax:

  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
     );

  }

And I know this is running because I can breakpoint it.


Specs: Windows 7 | Visual Studio 2010/2012 | Microsoft MVC3 | IIS Express


I've read all these SO posts, none seem to apply in this situation or do not help (mostly, because they apply to actual aspx pages, where I am trying to load pages via controllers):

The HTTP verb POST used to access path '/' is not allowed

The HTTP verb POST used to access path is not allowed

The HTTP verb POST used to access path '/Membership/user/' is not allowed

The HTTP verb POST used to access path '/Main/[object Object]' is not allowed

The HTTP verb POST used to access path '[my path]' is not allowed

HTTP verb POST used to access path '/' is not allowed in Facebook app


Any ideas?


Solution

  • The problem is here (due to my lack of experience with MVC/CSHTML):

    <form id="ScriptForm" action="/MyApp/ControllerName/ActionName/@ViewBag.IDNumber" method="post">
    ...
    </form>
    

    The MyApp used to work on my PC (I'm honestly not sure why it doesn't work now... maybe I had set something up for that name to resolve before?) but it no longer maps to anything. It DOES work on the server, but that's because there is indeed a mapping for "MyApp" in IIS. Simply changing this form to this:

    <form id="ScriptForm" action="@Url.Content("~/ControllerName/ActionName/" + ViewBag.IDNumber)" method="post">
    ...
    </form>
    

    ...does the job more elegantly and without errors.