ASP.NET MVC: Simply can't seem to get any output. help! - c#

In Visual Web Developer when I "run" my Controller (TestApp) I come up with this:
http://postimage.org/image/iggcs6hw/
I've tried adding "/TestApp" on the end of the local host address in the address bar and that gave me this result:
http://postimage.org/image/ih078cf8/
I don't think I've misspelled anything. Forgive me if this question is a stupid one, just trying to get my feet off the ground :D.

Make sure you have renamed the default ~/Views/Home folder that was generated when you created your project into ~/Views/TestApp folder. As far as the first error message is concerned make sure you have modified the default routes in Global.asax to make the TestApp controller the default one instead of Home:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "TestApp", action = "Index", id = UrlParameter.Optional }
);
So to sum up:
Make sure that you have an Index.aspx view inside the ~/Views/TestApp folder.
Make sure that you have set the TestApp controller in the default route in Global.asax
Make sure that your TestApp controller has an Index action
Now you will be able to call your application like this: http://example.com/ which will automatically call the Index action on TestApp controller which will render the ~/Views/TestApp/Index.aspx view.

Make sure your views are in the /Views directory. MVC follows a strict folder structure and this happens when it can't find something.
When adding a new view or controller to an MVC project, it's best to use the wizard provided for that purpose.

Related

Asp.net MVC Index page without domain paths

I believe this should have been asked and answered somewhere already, or is just a very basic thing, but I did not manage to find anything at all, I am guessing I might be querying my search wrong.
Either way, what I want is to display Index page without any domain paths.
What I want:
http://localhost:50024/
How I was able to make it with domain paths:
http://localhost:50024/Home/Index
I made a HomeController.cs and added a GET method for the Index view... which is in the Home folder under Views folder, and of course that creates domain paths. I do not care if I have to make extra controller or something, I just want it to display my index page without any paths. Thanks in advance!
You must set default values for the parameters in the route configuration in global.asax, similar to this:
routes.MapRoute( "Default", // Route nameĀ 
"{controller}/{action}/{id}", // URL with parametersĀ 
new { controller = "Home", action = "Index", id = "" } // Parameter defaults );
So that when the route parameters are missing, the routing system points the request to the desired controller and action.
just tested myself so i know it works
Test
obviously replace with what you have for the defaults in your routing if you have modified them or something
If all you're after is the "root" URL, you can also use ~/, like so:
Home
The Razor engine is smart enough to parse that URL in place, without needing to use the UrlHelper class.

C# View page URL

I've created a controller, called ClientController.cs and VS automatically created the necessary View files in /Views/Client. But I wanted to get these pages in a different URL... So, it is /Client but I need it at /admin/client.
What should I change?
Thank you!
It's not clear what your functionality will be in the long run, but here are a few options that allow you to get the URL format you want:
Perhaps you want a controller called "Admin" and an action called "Client". This would give you a path of /Admin/Client by default
Alternatively, you can change your route maps. For example, the following with route /Admin/Client to the Index of your Client controller:
routes.MapRoute(
"Default", // Route name
"Admin/Client/{action}", // URL with parameters
new { controller = "Client", action = "Index" } // Parameter defaults
);
Or maybe even go a far as using "Areas", depending on what you need. Have a Google of that if you're interested in learning more
If you want it to be admin/client, then using the default routing you should create an Admin Controller with an ActionResult method called Client. Your views folder should have an admin folder with your client view inside.
I haven't done a lot of MVC but i believe this is what you do.

MVC4 Multiple Controllers

This is a very basic question, yet I cannot find any clear, simple, direct answers.
I have a basic MVC4 app with 1 HomeController.cs file. I want to create a second Controller.cs file to put more code into so HomeController doesn't turn into spaghetti code.
So obviously step 1 is to add a new controller. I assume the next step is to add some stuff to RouteConfig.cs.
What do I need to add to RouteConfig.cs to utilize a new Controller.cs?
You shouldn't need to add anything. HomeController requires a line of code in your RouteConfig to be set as the default controller (for when users navigate to the site root), but any other controller should be accessible with the default routing.
Just create a controller, add some actions, and you should be able to route to it with the format Controller/Action or using the routing helper functions.
What does your routes file look like?
Normally, there's a default route:
routes.MapRoute("default",
"{controller}/{action}/{id}",
new { controller = "Home", action="Index" }
);
That means that so long as you add a new controller with the Controller suffix, MVC will make sure the routing engine sees your controller, and as long as your URL follows the above structure, requests made in that format will be routed to the appropriate controller.
We normally send it to a different view which submits to different controllers, or add a reference in your current controller if your just wanting to call certain methods in your current home controller.
What you really need first after creating a new controller is to add a new action (if it's not added automatically) and then add a new View for your new action.
You need to touch your routes only if you are about to process some specific parameters which dont match your default settings

How do I know which controller this page is hitting?

Pretty new to MVC I have a page on an open source application I have downloaded that is at the url...
http://localhost:51930/admin/login?databaseIssue=true
Obviously Im trying to find which controller and view this maps to in the application. How do I work this out? What should I search for and where to look?
Also how do I work out which actions process this view?
This should help you out. This tool is awesome!
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
This guide should get you started. Basically you work with a collection of routes and their arguments, in the global.asax.cs file. The guide there also has a section on custom routes.
By the defaulting routing rules, it's {controller}/{action}/
Which would make the controller in http://localhost:51930/admin/login?databaseIssue=true admin and the action Login.
By convention, MVC routes are generated in form
{app_base}/{controller}/{action}
Check out this stackoverflow question for more information.
So in your case, you'll want to look for an admin.cs class in your Controllers folder.
global.asax is where the route mapping is defined.
You'll see/set something like:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
so by default, your example maps to admin = {controller} and login = {action} and login action method would take the databaseissue=true bit as a parameter.
All these answers are good, except in the case where someone may have created a custom route to the specific url in question. By default, they are all correct, but if a custom route was setup, it could be going to the StackController and referencing the Overflow action.
Like Jamie R Rytlweski suggested above, reference RouteDebugger in your project, add the hook in your global.asax and try going to that page, it will show you a listing of all the routes defined in your application and then show you which routes the current page matches

Multiple Controllers with one Name in ASP.NET MVC 2

I receive the following error when trying to run my ASP.NET MVC application:
The request for 'Account' has found the following matching controllers:
uqs.Controllers.Admin.AccountController
MvcApplication1.Controllers.AccountController
I searched the project for MvcApplication1.Controllers.AccountController to remove it, but I can't find a match.
I try to registered a route to fix it:
routes.MapRoute(
"LogAccount", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = "" },
new string[] { "uqs.Controllers.Admin" } // Parameter defaults
);
but that didn't solve it.
Multiple types were found that match
the controller named 'Account'.
How I can fix this problem?
If you refactor your project and change the default Namespace and Assembly from "MVCApplication1" to "uqs", you may end up with 2 assemblies in your bin directory (the new one and the old one). You can get this error because the AccountController is in both assemblies.
Clean your bin directory of the old MVCApplication1.dll.
You can't have more than one controller named Account in your application, even in different namespaces.
You have to have these controllers split up by Area (a feature in ASP.NET MVC 2).
If you conduct a Find for AccountController you'll find all controllers named Account in your application; and move them off into different Areas if you want both of them, or delete one.
Had this same problem. Cleaned the bin and I was good to go.
A slightly confusing variation on the problem (similar in that it causes the same error message) can occur even with namespaces supplied. MVC 3 I think is a little pickier than MVC 2 on this front.
Short Answer:
Make sure the namespace of your controller is in fact the namespace specified in the MapRoute call!!
Long Answer:
I have 3 areas : default ("") / Facebook / Store and they each have AdminController
I have the route mapped like this (for my default area):
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Gateway", action = "Index", id = UrlParameter.Optional },
new string[] { "RR.Controllers.Main" }
);
A request to /admin gave the following error :
Multiple types were found that match
the controller named 'admin'. This can
happen if the route that services this
request ('{controller}/{action}/{id}')
does not specify namespaces...
The request for 'admin' has found the
following matching controllers:
RR.FacebookControllers.AdminController
RR.Controllers.AdminController
RR.StoreControllers.AdminController
But wait a minute! Didn't I specify the controller namespace.... ? What's going on.... ?
Well it turned out my default area's admin controller namespace was RR_MVC.Controller instead of Rolling_Razor_MVC.Controller.Main.
For some reason in MVC 2 this didn't give a problem, but in MVC 3 it does. I think MVC 3 just requires you to be more explicit when there's potential ambiguities.
AccountController is automatically generated by the ASP.NET MVC Visual Studio template. It is located in Controllers\AccountController.cs. Try searching for it in the project and delete it.
I had this problem...
Solved by removing a project reference in one of the .csproj files

Categories

Resources