Why is the wrong controller called when I start the ASP.NET MVC2 application?

I have a controller with a name MetricsControllerwith one action:

public class MetricsController
{
  public ActionResult GetMetrics(int id, string period)
  {
    return View("Metrics");
  }
}

I want to redirect calls to this controller as follows:

http://mysite/metrics/getmetrics/123/24h

I mapped an additional route in mine Global.asax.csas follows:

routes.MapRoute(
  "Metrics",
  "{controller}/{action}/{id}/{period}",
  new { controller = "Metrics", action = "GetMetrics", id = 0, period = "" }
);

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

I just added this to the default template project that Visual Studio 2010 creates.

When I launch the application, instead of using the default HomeController, it starts with MetricsController.

Why is this happening? There is nothing in the url when I run the application that matches the url pattern specified in the route Metrics.

All this is verified in Visual Studio 2010 using the integrated web server.

+3
3

, .

- - . routingata , - , .

- :

routes.MapRoute(
  "Metrics",
  "Metrics/GetMetrics/{id}/{period}",
  //assuming id, period aren't supposed to be optional
  new { controller = "Metrics", action = "GetMetrics" }      
);

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
+1

Metrics:


routes.MapRoute(
  "Metrics",
  "{controller}/{action}/{id}/{period}",
  new { controller = @"Metrics", action = "GetMetrics"}
);

MVC GetMetrics Metric URL-, .

+1

: Matrics, Matrics.

: , . , Metrics, ... , .

, , Metrics .

.

routes.MapRoute(
  "Metrics",
  "metrics/{action}/{id}/{period}",
  new { controller = @"Metrics", action = "GetMetrics", id = 0, period = "" }
);

HTHS,

:

URL-, , URL- , .

- URL url, ?

+1
source

Source: https://habr.com/ru/post/1768321/


All Articles