Get the url without the name of the method that called it - asp.net

I am try to get the url of the website plus the current controller without the method that called it.
Inside the ExampleController, ExampleMethod
Request.Url.ToString()
=>http://localhost:51747/.../ExampleController/ExampleMethod
I need
http://localhost:51747/.../ExampleController
The solution I would use is to parse and remove everything after the last slash, but I am not sure if there is already a method to do this using the server info.

Your issue can be solved by using one of the Url.Action HTML helper overloads. To generate your example URL:
http://localhost:51747/.../ExampleController
You would need to do:
#Url.Action("ExampleController", "Index", null, Request.Url.Scheme)
assuming you are using the default ASP.NET MVC Routing config.
In more detail, if the action you provide to the helper is one set as a default in the routeconfig, then it not be specified in the resulting URL.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
See the action = "Index" default? Whatever you have set there is what you will have to use when generating the URL.

Related

ASP.NET view routing default values

My default routing follows {controller}/{id}/{action} instead of the standard {controller}/{action}/{id}
Now i need to set up the defaults such that if no id is provided, default to Index action in controller
If Id is provided and no action is provided, default to Detail action
If both Id and Action is provided then route to the corresponding action.
How can I set up this routing?
currently:
routes.MapRoute(
name: "Default",
url: "{controller}/{id}/{action}",
defaults: new { controller = "projects", action = "Index" })
The usual approach would be to set up the routing just like you specified; i.e. create the following routes in order:
{controller}/
{controller}/{id}/
{controller}/{id}/{action}
Evidently, for the various routes, there are no optional parameters anymore. Apply defaults as usual. :)

How to shorten the URL to call directly an action instead of /controller/action

I have a HomeController.cs with some actions and can access these via http://localhost/MyWebSite/Home/Test
I'd like to shorten the URL to http://localhost/MyWebSite/Test
Therefor I added following code to the RouteConfig.RegisterRoutes method
routes.MapRoute(
name: "ShortendUrl",
url: "{action}",
defaults: new { controller = "Home", action = "Test" }
);
When trying to access it via http://localhost/MyWebSite/Test I see
HTTP-Error 403.14 - Forbidden
Where is my mistake? Is the MapRoute wrong? or is it maybe an issue at the IIS configuration?
For reference I used this blog entry: http://weblogs.asp.net/gunnarpeipman/asp-net-mvc-defining-short-urls-for-root-level-pages

Running an asp.net mvc project from a specified area

I've an asp.net mvc project with 2 different areas.
Here is my code in RouteConfig.cs below.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces:new[]{"Sample1.Controllers"}
);
When i run it, i'd like to see the Sample1 as a default project or vice versa. But it gives me an error that i specified below.
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 request for 'Home' has found the following matching controllers:AreaSample.Areas.Sample1.Controllers.HomeController AreaSample.Areas.Sample2.Controllers.HomeController
namespaces i specified doesn't work. Also Sample1AreaRegistration.cs is like that.
context.MapRoute(
"Sample1_default",
"Sample1/{controller}/{action}/{id}",
new { controller="Home", action = "Index", id = UrlParameter.Optional }
);
How can i solve that problem?
You shouldn't provide Area specific namespace for default route. Instead of having Sample1.Controllers in RouteConfig.cs file, change it to AreaSample.Controllers. And add namespaces in each AreaRegistration files.
To make Sample1 as default area, I assume, it is a separate project. So try adding it into configuration manager. Select the Sample1 project from solution configuration and run the application.
Or, try to add specific page as default in project properties. Right click on the project >> properties>> Web>>. Select Specific Page option and type the Areaname/ControllerName/ActionName
The best solution would be to not make Sample1 a separate area. But have Sample1 be the root Controllers/Views for the project (like the default template).
However, if you want to have 2 area like this, then you could probably do something like this:
DO specify the namespaces in the AreaRegistration classes.
Do NOTspecify the namespaces in the RouteConfig.cs class.
In the RouteConfig.cs, you should also provide a default area value.
routes.MapRoute(
name: "Root",
url: "",
defaults: new { area = "Sample1", controller = "Home", action = "Index" }
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

ASP.NET MVC - So I have a bad default route - on one machine

This should not be a problem, but somehow I am getting a 'resource cannot be found' for a default route. When I go to the IP of the site (by putting http://1.2.3.4 in my browser), I get the error for the following route:
http://1.2.3.4/Home/Home
This is somewhat strange to me, since I do not have a 'Home' action, but rather an 'Index' action on the 'Home' controller. I can navigate directly to other locations on the site - for example:
http://1.2.3.4/Products
http://1.2.3.4/Cart
http://1.2.3.4/Products/Query
http://1.2.3.4/Home/About
http://1.2.3.4/Home/Contact
Here are my route definitions (nothing out of the ordinary):
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have not changed my route config. I'm thinking that I somehow did something to my controller, but I can't for the life of me find it. Any help would be appreciated!
My advice would be to use Route Debugger. See Phil Hackk's blog on Route Debugger
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx/

ASP.NET MVC Url routing problems

I'm new to MVC and URL routing, and am looking for some guidance.
I would like to be able to have a URL per user, with the format:
http://www.example.com/username
So I added the following route:
routes.MapRoute(
"UserRoute", // Route name
"{username}", // URL
new { controller = "Users", action = "ViewUser", username = UrlParameter.Optional }
);
Which works great if it is the FIRST route, but it has messed up the default {controller}/{action}/{id} route. Now when I try to visit the root page, it tries to load my UsersController "ViewUser" action with a null parameter (or with "favicon.ico" as the parameter).
If I put my new route AFTER the default route, then MVC tries to find the controller called username and fails because it can't find it.
Is there any way to have URLs of the form {username} without mucking up the regular routing system? I could probably write a custom route for every controller I have, but that seems error-prone.
You can check out this link and see if it helps. It is also creating urls with just a username on them.
You can put a constraint to the allowed controller values for the default route, instead of creating a route for each controller.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL
new { controller = "controller1", action = "defaultaction", id = UrlParameter.Optional }, //default values
new { controller = #"controller1|controller2|controller3|..." }
);
when the controller does not match any of those it will try the next route

Resources