How do I know which controller this page is hitting? - c#

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

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

Is there a way to make the route mapping based on specific path

I code lots of ASP.NET but I'm kind of new with .net MVC, I've a default route registered like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And I want to add another Administrator area on the site and all the URL would be something like "http://localhost/Administrator/controller1", "http://localhost/Administrator/controller2", etc. I've lot of controllers in the Administrator namespace and I'm trying to register those controller with only one MapRoute, I did something like this:
routes.MapRoute("Administrator_default", "Administrator/{controller}/{action}/{id}", new { controller = "Administrator", action = "Index", id = "" });
it works with those controller but one problem is that in some other controller while I try to do a redirect like:
return RedirectToAction("Index", "Forum");
Then I'll always be redirect to http://localhost/Administrator/Forum instead of http://localhost/Forum, it's not a big issue but make the URL looks strange, I tried to restrict to certain namespace but it's not working. It looks just as I'm trying to register two default route and .Net just match the first one, I'm wondering is there a way to make it two default route and map on only specific path only?
This exact issue is why Areas were added to MVC 2. http://www.asp.net/whitepapers/what-is-new-in-aspnet-mvc#_TOC3_2
Agree with Zach's answer.
Not ideal, but you do have the option to have controllers in the controller root folder (e.g. /controllers/HomeController.cs) of your project as well as the controllers in Areas (maybe high level root pages that display menus for areas).
Secondly a quick tip on using the RedirectToAction method. You can specify the area you would like to redirect too using the route parameters e.g:
RedirectToAction("Index","Form", new { area = "MyOtherArea" });

ASP.NET MVC: how to make all unspecified URLs direct to one controller

I've got an ASP.NET MVC app that's working nicely with a handful of controllers, e.g. "Home", "Services" and "Go". The "Go" controller is where all the content is.
Now the marketing folks have come and said they don't want to see the word "go" in the URL. In other words, instead of:
http://mydomain.com/go/geography/africa
they want to have:
http://mydomain.com/geography/africa
I cannot create a controller for every path that they might want... so is there any way of writing my routing in Global.asax.cs in such a way that requests to "services" and "home" will be treated the normal way, but anything else will implicitly be routed to the "go" controller?
Are you on IIS7? It might be easiest to just implement URL rewriting on the server for this, rather than hacking about with your routes in Global.asax.cs.
EDIT: I've only ever done URL rewriting in Apache. For what it's worth that would be done using something like this:
RewriteEngine On
RewriteRule ^go/(.+)$ /$1 [R=301,L]
Have a look at http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/ and http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/. Hopefully they'll give you enough info to be able to get this working in IIS 7
Hey, I worked it out myself, without URL rewriting!
Inside RegisterRoutes() in Global.asax.cs:
routes.MapRoute("Services", "services/{action}/{*qualifier}",
new { controller = "Services", action = "Index", qualifier = UrlParameter.Optional });
// and other controllers that I want to work the normal way
routes.MapRoute("Default", "{*path}",
new { controller = "Go", action = "Index", path = UrlParameter.Optional });
And in my GoController class
public ActionResult Index(string path) { ... }
Works perfectly!
You could try adding a mapping that does "geography/{country}" and have it specify the controller manually and add the country as a parameter. I've seen it done to prevent things like "Dashboard/Dashboard" etc.
An example can be seen at Kazi Manzur Rashid's Blog - ASP.NET MVC Best Practices (Part 2) #15 for what I am describing.
Have you seen this: http://www.iridescence.no/post/Defining-Routes-using-Regular-Expressions-in-ASPNET-MVC.aspx ?
you could try mapping a route of "{action}/{id}" with a default set for the controller. But that'll also match anything of the form "{controller}/{action}" too - unless you can do some clever constraining.

Categories

Resources