MVC4 project blank page - asp.net-mvc

I've lost the last two hours trying to fix that problem. I can't remember what I've changed to my project but now it is returning a blank page no matter what the URL I write is. No controller is reached, I don't even get any single HTML tag.
I'm running on Windows 7 Starter and I use Visual Studio 2012 and IIS Express. Everything was working fine with this project, I was able to navigate and to access the controllers without any problem. I googled and found several articles suggesting to enable HTTP Routing, and similar HTTP modules in the Add/Removes functionalities section of the Control Panel. I checked all the stuff that are related to IIS but it's not working.
What I really don't understand is that first it was working without any problem and I can't remember which critical change I could have done to this project (I lastly worked on it 2 days ago) And secondly, others project are working just fine so IIS is properly configured. I checked in global.asax and RegisterRoutes is properly called. I haven't changed the default routing system.
I don't have any idea of what I should try since I don't get any error message. I would appreciate any kind of help. Thank you!
Update 1: It looks like the MVC framework is never called : nothing happens even if I try to debug and to put a breakpoint at the beginning of the Application_Start() in Global.asax
Update 2: Here is my RouteConfig.cs file :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
And here is my Application_Start function which is anyway never called according to debugging :
protected void Application_Start()
{
//ModelBinders.Binders.DefaultBinder = new PerpetuumSoft.Knockout.KnockoutModelBinder();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Utilisateurs", "ID", "AdresseMail", true);
}
(I tried to comment the useless lines (Websecurity since I'm not using it in the controller I'm trying to reach, Knockout and BundleConfig) but I got the same result). But this function is never called anyway.

I have the same problem and it turned out my _ViewStart.cshtml hadn't deployed to the server. I uploaded that and it worked. Check all your files are up correctly.

Related

namespace changing in ASP.net MVC [duplicate]

I currently have two unrelated MVC3 projects hosted online.
One works fine, the other doesn't work, giving me the error:
Multiple types were found that match the controller named 'Home'. This
can happen if the route that services this request
('{controller}/{action}/{id}') does not specify namespaces to search
for a controller that matches the request.
If this is the case,
register this route by calling an overload of the 'MapRoute' method
that takes a 'namespaces' parameter.
The way my hoster works is that he gives me FTP access and in that folder I have two other folder, one for each of my applications.
ftpFolderA2/foo.com
ftpFolderA2/bar.com
foo.com works fine, I publish my application to my local file system then FTP the contents and it works.
When I upload and try to run bar.com, the issue above fires and prevents me from using my site. All while foo.com still works.
Is bar.com searching from controllers EVERYWHERE inside of ftpFolderA2 and that's why it's finding another HomeController? How can I tell it to only look in the Controller folder as it should?
Facts:
Not using areas. These are two COMPLETELY unrelated projects. I place each published project into each respective folder. Nothing fancy.
Each project only has 1 HomeController.
Can someone confirm this is the problem?
Here is another scenario where you might confront this error. If you rename your project so that the file name of the assembly changes, it's possible for you to have two versions of your ASP.NET assembly, which will reproduce this error.
The solution is to go to your bin folder and delete the old dlls. (I tried "Rebuild Project", but that didn't delete 'em, so do make sure to check bin to ensure they're gone)
This error message often happens when you use areas and you have the same controller name inside the area and the root. For example you have the two:
~/Controllers/HomeController.cs
~/Areas/Admin/Controllers/HomeController.cs
In order to resolve this issue (as the error message suggests you), you could use namespaces when declaring your routes. So in the main route definition in Global.asax:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "AppName.Controllers" }
);
and in your ~/Areas/Admin/AdminAreaRegistration.cs:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "AppName.Areas.Admin.Controllers" }
);
If you are not using areas it seems that your both applications are hosted inside the same ASP.NET application and conflicts occur because you have the same controllers defined in different namespaces. You will have to configure IIS to host those two as separate ASP.NET applications if you want to avoid such kind of conflicts. Ask your hosting provider for this if you don't have access to the server.
In MVC4 & MVC5 It is little bit different, use following
/App_Start/RouteConfig.cs
namespace MyNamespace
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] {"MyNamespace.Controllers"}
);
}
}
}
and in Areas
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "MyNamespace.Areas.Admin.Controllers" }
);
Watch this... http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas
Then this picture (hope u like my drawings)
in your project bin/ folder
make sure that you have only your PROJECT_PACKAGENAME.DLL
and remove ANOTHER_PROJECT_PACKAGENAME.DLL
that might appear here by mistake or you just rename your project
What others said is correct but for those who still face the same problem:
In my case it happened because I copied another project n renamed it to something else BUT previous output files in bin folder were still there... And unfortunately, hitting Build -> Clean Solution after renaming the project and its Namespaces doesn't remove them... so deleting them manually solved my problem!
Check the bin folder if there is another dll file that may have conflict the homeController class.
Another solution is to register a default namespace with ControllerBuilder. Since we had lots of routes in our main application and only a single generic route in our areas (where we were already specifying a namespace), we found this to be the easiest solution:
ControllerBuilder.Current
.DefaultNamespaces.Add("YourApp.Controllers");
Even though you are not using areas, you can still specify in your RouteMap which namespace to use
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" },
new[] { "NameSpace.OfYour.Controllers" }
);
But it sounds like the actual issue is the way your two apps are set up in IIS
I just had this issue, but only when I published to my website, on my local debug it ran fine. I found I had to use the FTP from my webhost and go into my publish dir and delete the files in the BIN folder, deleting them locally did nothing when I published.
if you want to resolve it automatically.. you can use the application assambly
just add the following code:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { string.Format("{0}.Controllers", BuildManager.GetGlobalAsaxType().BaseType.Assembly.GetName().Name) }
);
There might be another case with Areas even you have followed all steps in routing in Areas(like giving Namespaces in global routing table), which is:
You might not have wrapped your Global Controller(s) in 'namespace'
you provided in routing.
Eg:
Done this:
public class HomeController : Controller
{
Instead of:
namespace GivenNamespace.Controllers
{
public class HomeController : Controller
{
You can also get the 500 error if you add your own assembly that contains the ApiController by overriding GetAssemblies of the DefaultAssembliesResolver and it is already in the array from base.GetAssemblies()
Case in point:
public class MyAssembliesResolver : DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
var baseAssemblies = base.GetAssemblies();
var assemblies = new List<Assembly>(baseAssemblies);
assemblies.Add(Assembly.GetAssembly(typeof(MyAssembliesResolver)));
return new List<Assembly>(assemblies);
}
}
if the above code is in the same assembly as your Controller, that assembly will be in the list twice and will generate a 500 error since the Web API doesn't know which one to use.
In Route.config
namespaces: new[] { "Appname.Controllers" }
Got same trouble and nothing helped. The problem is that I actually haven't any duplicates, this error appears after switching project namespace from MyCuteProject to MyCuteProject.Web.
In the end I realized that source of error is a global.asax file — XML markup, not .cs-codebehind. Check namespace in it — that's helped me.
i just deleted folder 'Bin' from server and copy my bin to server, and my problem solved.
Some time in a single application this Issue also come In that case select these checkbox when you publish your application
We found that we got this error when there was a conflict in our build that showed up as a warning.
We did not get the detail until we increased the Visual Studio -> Tools -> Options -> Projects and Solutions -> Build and Run -> MSBuild project build output verbosity to Detailed.
Our project is a .net v4 web application and there was a conflict was between System.Net.Http (v2.0.0.0) and System.Net.Http (v4.0.0.0). Our project referenced the v2 version of the file from a package (included using nuget). When we removed the reference and added a reference to the v4 version then the build worked (without warnings) and the error was fixed.
Other variation of this error is when you use resharper and you use some "auto" refactor options that include namespace name changing. This is what happen to me. To solve issue with this kind of scenario delete folderbin
Right click the project and select clean the project. Or else completely empty the bin directory and then re-build again. This should clear of any left over assemblies from previous builds
If it could help other, I've also face this error.
The problem was cause by on incorrect reference in me web site.
For unknown reason my web site was referring another web site, in the same solution.
And once I remove that bad reference, thing began to work properly.
If you're working in Episerver, or another MVC-based CMS, you may find that that particular controller name has already been claimed.
This happened to me when attempting to create a controller called FileUpload.
i was facing the similar issue. and main reason was that i had the same controller in two different Area. once i remove the one of them its working fine.
i have it will helpful for you.
I have two Project in one Solution with Same Controller Name. I Removed second Project Reference in first Project and Issue is Resolved
I have found this error can occur with traditional ASP.NET website when you create the Controller in non App_Code directory (sometimes Visual Studio prevents this).
It sets the file type to "Compile" whereas any code added to "App_Code" is set to "Content". If you copy or move the file into App_Code then it is still set as "Compile".
I suspect it has something to with Website Project operation as website projects do not have any build operation.Clearing the bin folder and changing to "Content" seems to fix it.

MVC view throwing 500.19 error when using the direct url, but not when routed via the controller?

I have two views in my mvc application. They are both stored within area, A, and within folder, home.
I recently was changing the properties of my project and it asked to reconfigure my site configs in iis as always I clicked yes. Since then I have been getting a 500.19 error saying that my configuration file has incorrect data.
At this point I was thoroughly confused. I compared my web.configs against other application web.configs and there really was no difference. So, I then changed my routing so that inputting this. :
http://stackoverflow/myarea/home
Actually returns my view of. :
http://stackoverflow/myarea/home/index
So, when I just input. :
http://stackoverflow/myarea/home/index => this errors with 500.19
but when I use. :
http://stackoverflow/myarea/home => this works with no error but brings up the same view as the above would
I have tried a lot of things blowing away my application in iis and recreating it, numerous iis resets, deleting my view and recreating it. Nothing has came close to working.
Now what is really odd is I have another view in the same folder. :
http://stackoverflow/myarea/home/myotherview
When I go directly to this url above it works perfectly no issues, no 500.19.
So, it is as if something is tying my one view to this issue. I know that this is an odd one, but I am lost on what to try next. Hoping someone has seen something like this before. I know a 500.19 is usually an issue within the web.config, but after the other page works I think it is some sort of setting I am just unaware of.
500.19 is a configuration issue.
See here http://blogs.msdn.com/b/webtopics/archive/2010/03/08/troubleshooting-http-500-19-errors-in-iis-7.aspx
I suspect that you do not have the routes in the global.asax or in the areas registration asax file setup properly. Without seeing your routes I cannot help you more specifically. But you might need to setup something like this in
public static void RegisterRoutes(RouteCollection routes)
routes.MapRoute(
"DefaultIndexRoute",
"{controller}/{action}/{id}",
new {controller = "Home", action="Index", id=UrlParameter.Optional}
);
}
in the AreaRegistrations.asax file it will be
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"DefaulAreatIndexRoute",
"MyArea/{controller}/{action}/{id}",
new {controller = "Home", action="Index", id=UrlParameter.Optional},
new [] {"My.Area.Namespace.Controllers"} // optional namespace in case there is a conflict
);
}
this is a catch-all kind of route. MVC will choose your route based on the first one that matches. So you will want this at the bottom of the list most likely. With more specific routes specified above it.
Basically if mvc cannot find an adequate route it will throw it back to IIS to figure out in which you will most likely get an error.
Please post your route configs so we can help you a bit more. Thanks.

HTTP 404 Error When starting ASP.Net Application using MVC 2

I am having problems trying to get a very simple ASP.Net application to start using .Net Framework 4 and MVC 2.
When press F5 in Visual Studio 2010, I get the following error message HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unable. Please review the following URL and make sure it is spelled correctly.
When I added the view, I added the view by right clicking on the method in the controller, and this of course added the view. I have also noticed that when selecting "Go to View", that this too throws an error in Visual Studio and says that the view does not exist! Further, I have gone to the Global.asax page and changed the default controller to be that of the one I have added, but this has made NO difference.
So, please tell me - but do I need to change?
Try to go to /ControllerName/ActionName. If you have changed the default, you have to make sure you have spelled it correctly. Also note that the ASP.NET MVC Framework removes the "Controller" suffix of controller names.
If your new controller is called MyNewController it should:
Inherit from Controller
Be called MyNewController
Like this
public MyNewController : Controller {
public ActionsResult MyAction() {
return View();
}
}
In Global.asax.cs for this case, the default settings counld be:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "MyNew", action = "MyAction" }
);
Note how the default controller setting lacks the "Controller" suffix.
To install MVC some considerations need to be take in account.
I think you are trying to install it on IIS 5 or 6
Please read this article
http://blog.stevensanderson.com/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
or upgrade to IIS 7
Had a similar problem. I had made the mistake of modifying the default route from this:
url: "{controller}/{action}/{id}",
To this:
url: "{controller}/{action}/{id*}",
Placing the asterisk in the wrong place caused absolutely every URL to give me a 404 - and as usual ASP.Net routes are practically impossible to debug.
The solution was placing the asterisk in the proper place:
url: "{controller}/{action}/{*id}",
(This allows routes with as many forward slashes as you like, instead of limiting them to 3. The portion with an asterisk is a catchall.)

ASP.NET MVC Deployment Problem

I have deployed my application to a server running IIS6 using the method which invloves changing the routes to:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
And adding a handler in IIS for .mvc extentions. This is working fine for the most part until I add the [Authorize] attribute to HomeController class.
This ends up in the app trying to redirect the user to the logon page which is what I expect however the logon page URL is shown as http://server/virtualdir/Account/LogOn?ReturnUrl=%2fvirtualdir%2fDefault.aspx
This is causing a problem as no .mvc extension is being added to the Account controller part of the URL.
The problem has been solved by changing the following in web.config:
<authentication mode="Forms">
<forms loginUrl="~/Account.mvc/LogOn" timeout="2880" />
</authentication>
Not directly an answer to your question, but from my experience it worked just fine to deploy an application with the new routing features just as it is to IIS6 and add a wildcard mapping to aspnet_isapi.dll. Then you can use any URL you want, and nobody will notice when you change to a newer version in the future.
Yes, static file handling is theoretically less efficient this way, but you will need really a lot of traffic to notice anything. And if you really get a lot of traffic, you still could and even should move all your static files to a another domain/subdomain (or even a CDN) anyway, like stackoverflow.com does. It can still point to the same server, you just use different IIS settings for this subdomain site. But with e. g. just a few thousand visitors per day you don't even have to think about it.

ASP.NET MVC Routing in Azure

I have an Azure Web Role project that was recently MVC'd by another developer. According to the developer the app works with no problem when run on it's own (i.e. as a simple web app). However, when I try to run it in the context of the Azure cloud service, I'm seeing a number of 404 errors. I suspect something is not quite right with the routing. Here's an abbreviated version of the current RegisterRoutes method that's part of Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{Services}/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Configuration",
"Configuration",
new { controller = "Configuration", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Account", action = "Index", id = "" }
);}
When the app starts up, the correct view from the Account controller's Index action is displayed. However if I try to navigate to Configuration I get a 404. Converesly if I change the method to this:
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{Services}/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Account",
"Account",
new { controller = "Account", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Configuration", action = "Index", id = "" }
);}
I get the correct view from the Configuration controller's Index action, but I can't navigate to the Account view.
I'm guessing this is a simple problem to solve, but not knowing what exactly was done to "MVC" the Azure app and being new to MVC has me beating my head into the wall.
Here's the configuration of the machine where I'm encountering this issue:
Windows 7 Ultimate with IIS 7.0
Visual Studio 2008 SP1
ASP.NET MVC 1.0
Windows Azure SDK 1.0
Thoughts?
Try using my Routing Debugger. It can help you understand what's going on. http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
It's weird that the behavior would be different locally than in Azure. Also, you should post your controller code (remove the contents of the action methods, we just need to see the method signatures).
If I had to make a wild guess, I'd guess your Configuration route (in the first example you gave) needs to add id="" in the defaults section.
Haacked: Thanks for pointing me to the debugger. That helped me hunt down the issue in a matter of minutes.
The answer was much simpler than I thought. It all had to do with the following line of code:
routes.IgnoreRoute("{Services}/{*pathInfo}");
I put this line in to help resolve an issue I was having with ASP.NET MVC and WCF RIA Services (more info on that here). The curly braces shouldn't be there. I don't want to replace Services. The code should look like this:
routes.IgnoreRoute("Services/{*pathInfo}");
You can read a full write-up here.
I don't think this is your problem, but you might verify that the System.Web.Mvc reference has its Copy Local = true.

Resources