I have problems with generating URL with Html.RouteLink method using route.
My routes:
routes.MapRoute(
name: "DefaultPage",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "MessagesIndex",
url: "{controller}/{page}",
defaults: new { controller = "Messages", action = "Index", page = UrlParameter.Optional }
);
My controller(MessagesController) index method header:
public ActionResult Index(int page = 0)
And link generation:
#Html.RouteLink("Messages", "MessagesIndex")
I already tried ActionLink and Action...
EDIT: Sloved. Glimpse helped and MSDN. Its very important order of routes too.
Can you explain what problems you are having?
Maybe this will work:
#Url.Action("Index", "Messages")
Related
I am using Asp.net Mvc 5 with C#.
I want to disable default routing in my project. My map routes like;
routes.MapRoute(name: "News",
url: "haberler",
defaults: new { controller = "News", action = "Index"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When a user visits my website's news page, it's like;
www.domain.com/haberler
But also the user can visit the news page as below;
www.domain.com/news
I want to remove that "/news" or direct to seo-friendly url like; "/haberler"
So how can I disable default routing (Controller-Name-Convension) routing?
Instead of removing the "default" route, you can add a controller constraint to it
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = #"(Account|Manage|Home)" }
);
Like this, /news will return 404 Not Found.
In MVC, the convention is to map URLs to a particular Action on a particular Controller. Remove default route to disable default convention:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
www.domain.com/news will not match in any of your routes hence it will throw an error.
Try removing with this
RouteTable.Routes.Remove(RouteTable.Routes["NAME ROUTE YOU WISH TO RMOVE"]);
Hope this will helps you.
I know it's more of the same (SO has more than 5,600 questions on this), but I've been sitting on this one for a couple of days now so I guess it was the right time to ask a question.
My requirements
I want to have the following routes in my asp.net mvc app:
myapp.com/sigin -> Controller = Account, Action = SignIn
myapp.com/signout -> Controller = Account, Action = SignOut
myapp.com/joe.smith -> Controller = User, Action = Index
myapp.com/joe.smith/following -> Controller = User, Action = Following
myapp.com/joe.smith/tracks -> Controller = Tracks, Action = Index
myapp.com/about -> Controller = About, Action = Index
Any other default route, so that's why I left the standard one there.
My Code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "About",
url: "about",
defaults: new { controller = "About", action = "Index" }
);
routes.MapRoute(
name: "MyTracks",
url: "{username}/tracks",
defaults: new { controller = "MyTracks", action = "Index" }
);
routes.MapRoute(
name: "MyTunes",
url: "{username}/tunes",
defaults: new { controller = "MyTunes", action = "Index" }
);
routes.MapRoute(
name: "MyProfile",
url: "{username}",
defaults: new { controller = "User", action = "Index"},
constraints: new { username = "" }
);
routes.MapRoute(
name: "Account",
url: "{action}",
defaults: new { controller = "Account" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Issue
Routes number 3 and 4 just don't work as they get mixed up with route 1 and 2. I have tried debugging my code with Phil Haack's routing debugger, but no luck. Any ideas what I am doing wrong?
The problem is in your last two custom routes. All the routing framework has to work with is just a single token from the URL, and two possible routes to match it with. For example, if you attempt to go to the URL, /signin, how is the routing framework supposed to know there's not a user with username "signin". It's obvious to you, a human, what should happen, but a machine can only do so much.
You need to differentiate the routes in some way. For example, you could do u/{username}. That would be enough to help the routing framework out. Short of that, you'll need to define custom routes for each account action before the user route. For example:
routes.MapRoute(
name: "AccountSignin",
url: "signin",
defaults: new { controller = "Account", action = "Signin" }
);
// etc.
routes.MapRoute(
name: "MyProfile",
url: "{username}",
defaults: new { controller = "User", action = "Index"},
constraints: new { username = "" }
);
That way, any of the actual action names will match first.
I want to have the following:
Link to {controller}/{destination} and link to {controller}/{action}, for example: flights/berlin and flights/search accordingly.
My routes config is as follows:
routes.MapRoute(
name: "LandPage",
url: "{controller}/{destination}",
defaults: new { controller = "Flights", action = "Index", destination = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If "LandPage" is first, the route will go always to the land page with the url parameter (i.e. --> flights/search will go to flights/index with parameter destination = search) and its bad for me.
If "Default" will be first, and I try to navigate to flights/berlin, it will try to navigate to the flights controller and action = berlin, of course no such action...
The only solution I can think of is using "LandPage" first, and compare the {destination} parameter with name of action and redirect to that action... I don't like that solution... anyone can think about another solution??
Thanks!
You can set fixed routs for specific actions:
routes.MapRoute(
name: "Search",
url: "Flights/Search/{search}",
defaults: new { controller = "Flights", action = "Search", search = UrlParameter.Optional }
);
and
routes.MapRoute(
name: "LandPage",
url: "Flights/{destination}",
defaults: new { controller = "Flights", action = "Index", destination = UrlParameter.Optional }
);
before your default route.
if i have routing:
routes.MapRoute(
name: "Default",
//url: "{action}",
url: "{controller}/{contynent}/{country}",
defaults: new { controller = "Home", action = "Index", kontynent=UrlParameter.Optional , panstwo= UrlParameter.Optional }
);
now in my page i can use:
localhost:4084/Home
localhost:4084/World
localhost:4084/World/Europe
localhost:4084/World/Europe/England
but in this case i can use only action equals Index and #Html.ActionLink don't give any results :( (moves me to: http://localhost:4084/?action=Action&controller=Controller).
How change MapRoute to form, where i can use:
localhost:4084/Home/About (controller=Home, action=About)
localhost:4084/Home/SignIn (controller=Home, action=SignIn)
localhost:4084/World/Europe (controller=World, action=Index, contynent=Europe)
localhost:4084/World/Europe/England (controller=World, action=Index, contynent=Europe, country=England)
Best way i want to create exception for World controller in MapRoute.
Default of asp.net-mvc4 is http://domainname.com/products/1 with routes
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
and I want to rewrite to http://domainname.com/products/1.html that has .html extention .
Any ideas for this?
Do you mean this:
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}.html",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
Why would you want to put .html extension to a query string or route parameter?
if you take a website with html files, the extension is with the page not with the parameters. Don't know if you have any specific requirement to put it always at the end of the url. but it doesn't make any sense.
routes.MapRoute(
name: "Products",
url: "{controller}/{action}.html/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
you might have problems with iis after doing the above, please take a look at this discussion as well.
ASP.NET MVC Routing - add .html extension to routes