I have an ASP.NET Web Forms APP, I want to migrate it to ASP.NET MVC a piece at a time. I have setup MVC to run within the webforms app. I have everything set up appropriately such that if I put my ASP.NET MVC folders (Controllers, Views) in a subfolder called MVC in the project, and set my routes in my global asax to
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute("Default", // Route name
"w/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
All of the config and assembly references are setup. If I debug I can hit a breakpoint that is inside my Index method, on my Home controller.
public class HomeController
: Controller
{
public ActionResult Index()
{
this.HttpContext.Trace.Write("Hrm...");
return View("index", (object)"Hello");
}
}
The problem that is occurring is that
return View("index", (object)"Hello");
Does not return an error status code or the search list you’d expect typically if MVC can’t locate the view. Instead I am getting a 200 http response, and nothing in the content body of the response.
Here are the http request details:
GET http://localhost.:2396/w/home/index2 HTTP/1.1
Accept: */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; MS-RTC LM 8; InfoPath.2; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: localhost.:2396
Pragma: no-cache
Cookie: ASP.NET_SessionId=3yqf2t55sckemhmxq2bhibmq
HTTP/1.1 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: Wed, 09 May 2012 12:42:43 GMT
X-AspNetMvc-Version: 2.0
Cache-Control: private
Content-Length: 0
Connection: Close
That request above goes to index2, which is an action that doesn’t exist and it doesn’t give me an error. I suspect the ViewResult in the index action is throwing an exception but somehow it’s getting supressed somewhere. I inherited this code base, so I’m just trying to figure out whats going on.
In the web.config i’ve configured the error handling such that i should be able to see any errors messages, but that still doesn’t explain why the http status code is always 200.
<customErrors mode="Off"/>
<httpErrors errorMode="DetailedLocalOnly"/>
Another quick note, is if i replace
return View("index", (object)"Hello");
with
return Content("abc");
It will correctly output “abc” in the content body of the http response.
Any ideas?
Seems like your view is in wrong location because you mentioned that Controller, Views are under a folder called “MVC”.
The “Views” folder should be under the root directory, that is the place where ViewEngine looks for the views.
Update:
You can override this default behavior using a custom ViewEngine. For that implement
IViewEngineinterface and it has got a method calledFindViewwhere you can implement your own logic to scan different location.