ASP.Net MVC 4 Route/Routing performance issue - asp.net

Performance issue with routing.
The asp.net MVC 4 application takes more time to find the controller itself.
Our project structure for route is area/controller/action/id
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{area}/{controller}/{action}/{id}",
defaults: new { area = "QQQQQQQ", controller = "Home", action = "Index", id = UrlParameter.Optional });
}
unlike the find view this routing takes approximately the same time for all the request(second time , third time and etc)

Related

Why doesn't this route work if I change its order in RouteConfig?

When I access this route http://localhost:60015/myroute, it does not work if the order of the route like so:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"myroute",
"myroute/{name}",
new { controller = "myroute", action = "search", name = "" }
);
}
It works if I reverse the order. What does that mean? How does that happen?
This is because The order of the routes that you add to the route table is important. Your new custom route should be added before the existing Default route. If you reversed the order, then the Default route always will get called instead of the custom route.

How to use short urls for categories in MVC

Short urls containing product categories like
http://example.com/Computers
Should used in ASP.NET MVC 4 shopping cart.
If there is no controller, Home Index method with id parameter as Computers should called.
I tried to add id parameter to home controller using
public class HomeController : MyControllerBase
{
public ActionResult Index(string id)
{
if (!string.IsNullOrWhiteSpace(id))
{
return RedirectToAction("Browse", "Store", new
{
id = id,
});
}
return View("Index", new HomeIndexViewModel());
}
but http://example.com/Computers causes 404 error
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make
sure that it is spelled correctly.
Requested URL: /Computers
Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.6.1073.0
How to force some controller call if there is no controller defined after slash in http://example.com/....
MVC default routing is used:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
It looks like MVC ignores routing parameter:
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Howe to fix this ?
your problem is because aspnet mvc is trying to find a controller with the name Computers, and your controller is Home, you can add a new route like this before the route with name Default:
routes.MapRoute(
name: "Computers",
url: "Computers",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
In the above case, you are creating a route with match with the url http://domain.com/Computers and this route will be manage by the HomeController.
Also, according to your comment, you can have a route like:
routes.MapRoute(
name: "Default",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Invalid url in form rendered by html.beginform

I have an application which is mix of WebForms and MVC.
HomeController is a simple controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
On view - /Views/Home/Index.cshtml, I have used Html.BeginForm to render the form.
#using(Html.BeginForm("Index", "Home", FormMethod.Post, new { #class = "form-horizontal"}))
{
#Html.TextBox("username", "");
#Html.Password("password", "");
<button type="submit">Login</button>
}
Routes are configured like below. First 2 routes are using MapPageRoute to map to webforms.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("admin", "admin", "~/admin.aspx");
routes.MapPageRoute("adminEdit", "adminEdit", "~/adminEdit.aspx");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But after my homepage ("Home" is my default controller) is rendered to the browser, form action has url:
/admin?action=Index&controller=Home
not "/Home/" even though i have passed "Index" & "Home" as actionname and controllername to Html.Beginform respectively.
Url routing is working fine when i browse to my homepage. But the form's "action" has invalid url. It seems like it is matching the first route i have configured. Why?
I got it to work by following the instructions in this post:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute(
routeName: "admin",
routeUrl: "{pagename}",
physicalFile: "~/{pagename}.aspx");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
However, I cannot explain why the parameter is necessary to make it not match in this case.

asp.net mvc 4 controller routing depending on url

Depending on url, I want to route different controllers. So depending on different URLs directed to same DNS server, I want to give my website a different look and feel.
To test this locally I tried:
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 = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"localhost",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Contact", id = UrlParameter.Optional }
);
}
}
Wat I wanted was the Contact page to appear, not the default Home Page as the URL was :/localhost:portnr./.
How can I get the first bit of an URL (domain) to decide which controller to route?
Suppose you have the Contact controller and Index Action, and you want to load that on page load,
you can specify as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Contact", action = "Index", id = UrlParameter.Optional }
);

ASP.NET WEBAPI add custom route

I have the new .NET WebAPI up and running and I am trying to add a new route to my project so that I can see the SettingsController in action. I have added my route to the Global.asax file within the RegisterRoutes method but I receive a 404 error when I try and browse to: sampleUrl:12345/settings/index. I am able to see: sampleUrl:12345/home/index without issue.
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Settings",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Settings", action = "Index", id = UrlParameter.Optional }
);
}
Do I need to have an ActionResult Index() in my SettingsController?
Your settings route is redundant. What you are probably missing is the index method in settings controller.

Resources