Some background:
I create a new ASP.NET MVC 3 WebApplication.
Then I add a Webforms page: ~/ASPWebforms/Test.aspx
Then I edit the routing in the Global.asax file like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Test", "Test/", "~/ASPWebforms/Test.aspx");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Now I type in my browser http://localhost:54847/Test and everything works as expected.
The problem is that all other links look like that: http://localhost:54847/Test?action=Index&controller=Home
I also tried to change the order of the routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapPageRoute("Test", "Test/", "~/ASPWebforms/Test.aspx");
}
but then http://localhost:54847/Test won't work anymore.
I'm expecting the link http://localhost:54874/Test to go to ~/ASPWebforms/Test.aspx
And the other links work as usual mvc style {controller}/{action}/{id}
Thx for your help!
I can help with the second doubt.
The reason why it wont work is because the routes order is important.
http://msdn.microsoft.com/en-us/library/cc668201.aspx
whenever it finds a match, no more routes will be tested to that request.
I found a solution for the first problem on this page:
http://forums.asp.net/p/1589809/4028028.aspx
An interesting sentence from this page:
Please read the topic "Understanding the Outbound URL-Matching
Algorithm" in Steve book.
Related
I'm working with ASP.NET MVC3, I created my routes and compose my urls but I have something weird in my url after navigated in browser.
Url actually rendered: /controller/action/page.html
Url navigated: /controller/action/page.html#.V7cdQJh97cw
I think that maybe is SessionID but I can't know how to remove it.
Appreciate any helps!
Go to the RegisterRoute method of RouteConfig.cs file in the App_Start folder of your project where Default Routing rule is written there.
Route Rule:- ControllerName/ActionName/id
where id is optional. So you want to remove Id parameter ,then make Id=" ".
for example:-
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
What Is the proper syntax to put in the RouteConfig.RegisterRoutes method in an ASP.NET MVC application if I want the app to ignore all urls that start with the word "score" like
http://myserver/score*.*
?
In other words, any url that starts with the text "score" I want my app to ignore.
I tried:
routes.IgnoreRoute("score*.*/{*pathInfo}");
I also tried several other combinations of this syntax but can't quite get it right.
Here is what I have so far in my RouteConfig. It's pretty much the standard stuff.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Order", action = "Add", id = UrlParameter.Optional }
);
}
}
You should place your score*.* as a regEx expression in IgnoreRoute:
routes.IgnoreRoute("{score}/{*pathInfo}", new { score = #"score*.*" });
For more general answers, you can use this pattern to ignore routes that you want:
Routes.IgnoreRoute("{*foo*}", new { foo = #"someregextoignorewhatyouwant"});
So for score.js and score.txt route you will add regEx that filters that routes.
I am working on existing asp.net website and I get the code from client server where the code was hosted. I was able to run the project in my local pc but when I try to add new controller function then it just show http 404.
![enter image description here][1]
For example in AccountController.cs there was a existing login function
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
if (WebSecurity.IsAuthenticated == true){
WebSecurity.Logout();
Session.Abandon();
}
ViewBag.ReturnUrl = returnUrl;
return View();
}
Then I copied this function and created a new one like below and also duplicated the view file and renamed
//
// GET: /Account/AaLogin
[AllowAnonymous]
public ActionResult AaLogin(string returnUrl)
{
if (WebSecurity.IsAuthenticated == true)
{
WebSecurity.Logout();
Session.Abandon();
}
ViewBag.ReturnUrl = returnUrl;
return View();
}
Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
routes.MapRoute(
"AaLogin", // Route name
"Account/AaLogin", // URL with parameters
new
{
controller = "Account",
action = "AaLogin",
id = UrlParameter.Optional
});
}
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ViewListingNoAuth",
url: "{id}",
defaults: new { controller = "Listing", action = "PublicView"}
);
routes.MapRoute(
name: "socialMediaHooks",
url: "externalService/uploadFlyerToSocialMedia/{id}/{sName}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
But it still doesn't work when I browse /Account/AaLogin... any body can help me in this or any resource so I can understand.
If anyone can give me a idea whats happening I would be very happy.
Here are a few troubleshooting steps you can try:
Observe the call in Fiddler. Is the call being fired? If so, does it match what you are expecting?
Install RouteDebugger (using NuGet) and enable it. This is a great tool to help you see what's going on with your routes. It will tell you which routing rules are being hit.
P.S. You may want to use one of the "asp.net-mvc" tags for similiar questions.
Some just changing the route rule order can solve the problem. i had to do it one time to get my application find the route. Try to change the route order in your Global.asax.cs file. Put the login route before the default route. sometime it could be the problem !
I am posting this answer just for the reason that it might help other begginer people. When I was working on the above problem I was almost a begginer in asp.net. But later on I found the reason for it but forgot to update the result. Problem was that client sent me the already builded version not the source code. Later on I got the full source code and was able to make modifications.
I have added WCF service to MVC 5 application, and created a route for it:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new ServiceRoute("Service1.svc", new ServiceHostFactory(), typeof(Service1)));
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
The problem is that all my links leads to the Service1.svc route now. #Html.ActionLink("Passport Maker", "Index", "Home", new { area = "" }, new { #class = "navbar-brand" }) become http://localhost:50099/Service1.svc?action=Index&controller=Home and other links change in the same way.
If I add ServiceRoute after "Default" route, the links work correctly but the service is unavailable.
Why it happens (there is no "Service1" in the links, why they select the service route then?) and how to fix it?
The solution:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = "^(?!Service1.svc).*" }
);
routes.Add(new ServiceRoute("Service1.svc", new ServiceHostFactory(), typeof(Service1)));
Explanations for those who may encounter a similar problem: the reason of the problem was that Html.ActionLink uses the first matching route to generate the link. And my service route was the first and was matching, because a route does not require to include {controller} and {action} parameters to be matched (as I thought initially).
The solution is to put the default route first, so it is used by Html.ActionLink. And to still be able to use the service route, need to exclude it from the the first route using constraints. The regex ^(?!Service1.svc).* matches only those controller names that don't start from "Service1.svc".
How do I handle url like this:
{subdomain}.{domainname}/{areas}/{controller}/{action}
For example: user1.contoso.com/Manage/User/View
I want it to be route:
{area} = Manage
{controller} = User
{action} = View
{username} = user1 // View action parameter
Any help would be greatly appreciated :-)
Generally routes are defined in the file Global.asax in the method RegisterRoutes
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Export", // Route name
"Export/{action}/{table}", // URL with parameters
new { controller = "Export", action = "AsExcel", table = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
With a definition like above you can use a separate route for ExportController such that the default action is AsExcel
in the case of route as the one you have described, I am not sure if Areas is part of the MVC pattern.
Still for what it is worth, you may have some benefit from this thread
ASP.Net MVC with complex routes - how to keep it "sane"?