how to obsolete controller name from Html.ActionLink in mvc3 - c#

I want to obsolete controller name from Html.ActionLink
because I defined my controller in route how to do this,
because if I leave controller name blank in Html.ActionLink
mvc3 automatically put current controller name in Action Link.

If you named your route, you can use RouteLink instead of ActionLink.
You'll only have to specify the name of the route, not the controller.
Here's an example of a named route:
routes.MapRoute( "myRoute",
"doStuff/Now",
new {controller = "MyController", action = "DoIt"} );
And here's how to use it in your view
Html.RouteLink( "Do it!", "myRoute" );
Please see MSDN for full details on RouteLink.

Related

How to route to url.com/ControllerA/ControllerB/ControllerB's action in MVC

So I have a ProjectsController with the default route url.com/projects/action and I have a Controller for each Project like MosaController. The URL for the project should be url.com/projects/{ProjectName}/action, so in the Mosa example url.com/projects/Mosa/action.
I have set up a route in the RouteConfig that can solve this problem
routes.MapRoute(
name: "ProjectViewRoute",
url: "projects/{controller}/{action}",
defaults: new { controller = "Projects", action = "Index" }
);
This works I can call url.com/projects/action and url.com/projects/Mosa/action and the correct controller is selected, but when I call the URL url.com/{ProjectName}/action it invokes the action, because of the default route. Is there a way to ignore the default route? Or is a there better way of concatenating controllers like this?
Thanks!
Do you need the default route for other portions of your app? If not, why not simply remove it and just have the ProjectViewRoute? Or consider creating "Projects" as an MVC "Area" (https://www.c-sharpcorner.com/article/getting-started-with-area-in-asp-net-mvc/). This would eliminate the project-specific route outside of the Projects area.

Change Controller Route in ASP.NET Core

So I have a HomeController, to access it along with Actions I have to type url.com/home/action.
Would it be possible to change this to something else like url.com/anothernamethatpointstohomeactually/action?
I suggest you to use attribute routing, but of course it depends on your scenario.
[Route("prefix")]
public class Home : Controller {
[HttpGet("name")]
public IActionResult Index() {
}
}
This will be found at url.com/prefix/name
There are a lot of options to attribute routing, some samples:
[Route("[controller]")] // there are placeholders for common patterns
as [area], [controller], [action], etc.
[HttpGet("")] // empty is valid. url.com/prefix
[Route("")] // empty is valid. url.com/
[HttpGet("/otherprefix/name")] // starting with / won't use the route prefix
[HttpGet("name/{id}")]
public IActionResult Index(int id){ ... // id will bind from route param.
[HttpGet("{id:int:required}")] // you can add some simple matching rules too.
Check Attribute Routing official docs
You can add new Routes in your Startup.Configure method within your app.UseMvc(routes => block:
routes.MapRoute(
name: "SomeDescriptiveName",
template: "AnotherNameThatPointsToHome/{action=Index}/{id?}",
defaults: new { controller = "Home"}
);
The code is quite similar to ASP.NET MVC.
For more info, see Routing in ASP.NET Core.
Below is for ASP.NET MVC (not ASP.NET Core MVC)
You can also add a new Route via routes.MapRoute in your RouteConfig:
routes.MapRoute(
name: "SomeDescriptiveName",
url: "AnotherNameThatPointsToHome/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Make sure, you insert the code before you define your Default route.
For more information, visit the docs.
Using the Route attribute on top of your controller will allow you to define the route on the entire controller.
[Route("anothernamethatpointstohomeactually")]
You can read more here.
In ASP.NET Core 6, we just do that in one line.
Go to your Controller and write before your action method:
[Route("YourController/YourAction/{YourParameter?}")]
In your example, you you need to write like this:
[Route("Home/Index/{name?}")]
You can change your url by modifying your routing configuration.
It is kind of like htaccess but not really.
https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/creating-custom-routes-cs
Another solution is to create a page and do a server redirect.
Server.Transfer

ASP.NET Core Route Tag Helper use Route?

I'm kind off confused about how the asp-route-* tag helper works. What I understand is that it's kind of bound to the routing I've setup. E.g.
routes.MapRoute(
name: null,
template: "{category}/Page{page:int}",
defaults: new { controller = "Product", action = "List" }
);
Here I map my route as follow: /Category/PageNumber for the Action "List" in Controller "Product"
The following code will, when clicked follow the previous maproute
<a class="btn btn-block
#(cat == ViewBag.SelectedCategory ? "btn-primary" : "btn-default")"
asp-controller="Product"
asp-action="List"
asp-route-category="#cat"
asp-route-page="1">#cat</a>
So how I understand it is that "asp-route-category" will search for "{category}" in my routeMap template, and then "asp-route-page" will search for "{page}" in the routeMap template ?
The documentation on MS is kind off confusing or just to abstract, can someone confirm or explain this in a better way ?
When using conventional routing, as you are doing, the controller and action parameters are required and map to your controller and action name.
So if you want to route to the Action List in Controller Product with a
Category and a Page parameter, your routing should be like the following:
routes.MapRoute(
name: null,
template: "{controller}/{action}/{category}/Page{page:int}",
defaults: new { controller = "Product", action = "List" }
);
Update:
The asp-route attribute in the Tag Helpers you are using, will provide parameters to route values.
So basically, asp-route-MyParameter will add MyParameter to route values with the specified value.
More Information here.

rewriting urls for simple html pages in asp.net MVC app

I have asp.net mvc application.
And I have html page. called lp.html.
i want to rewrite page name url from lp.html to something else, lets say appndomainname.com/location
how can I do this, because in this case I am dealing with simple html pages. No controller or controller method.
So code like this won't be needed
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
any help will be appreciated, thanks

Why does my route get redirected, this doesn't make any sense to me

I have these 2 routes mapped out:
routes.MapRoute(
"Admin",
"admin/{controller}/{action}/{id}",
new { controller = "Admin", action = "index", id = "" }
);
and then I have:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
So the 2 routes are identical, except the first one has /admin prefixed in the URLS.
This is what is happening, I have no idea how to explain this:
When I go to:
www.example.com/user/verify
it redirects to
www.example.com/admin/user/complete
instead of
www.example.com/user/complete
The action Verify simply redirects to Complete like this:
return RedirectToAction("complete", "user");
And all the complete action does is populate the ViewModel, and then calls the view.
How can it be redirecting and adding the prefix /admin/ to the URL?
I believe it is redirecting to the Admin route because the Admin route is the first with all the matching parameters (controller and action in the case provided). If you want to use something like this you will need to either look into using areas (MVC2) or using a named route redirect.
admin is your controller, you dont need an admin/controller/action the default route works just fine
all you need is an admin controller and the default route will find it for you
ie {controller}/{action}/{id}
will send /admin/addproduct to a controller named admin and an action called addproduct
you only need to add routes if you want something custom for example
/products/televisions/hdtv/2
where products would be a controller and the last 3 are category,subcategory and pagenumber
on the controller you point it to within your route.
hope that makes sense
Not sure exactly how your controllers are structured, but you can add a constraint to the first MapRoute to limit it to the specific controllers you want the route to apply to:
routes.MapRoute(
"Admin",
"admin/{controller}/{action}/{id}",
new { controller = "Admin", action = "index", id = "" } ,
new { controller = "[Some regex Expression - e.g. Admin]" }
);
Which will make the route only applicable for those controllers related routes. You can also use this tool to debug your routes. Depends how you have things structured, but like #NickLarson said - sounds like your using area functionality of MVC 2.
mvc goes from top to bottom while matching router, that' why you are dealing with this problem

Categories