Use NuGet routing debug package (#17572)
* Use NuGet routing debug package * Use NuGet routing debug package * Use NuGet routing debug package * Use NuGet routing debug packagepull/17579/head
parent
994ad6c7ec
commit
fa4c1e7b6e
|
@ -5,7 +5,7 @@ description: Discover how ASP.NET Core routing is responsible for matching HTTP
|
|||
monikerRange: '>= aspnetcore-2.1'
|
||||
ms.author: riande
|
||||
ms.custom: mvc
|
||||
ms.date: 3/25/2020
|
||||
ms.date: 4/1/2020
|
||||
uid: fundamentals/routing
|
||||
---
|
||||
# Routing in ASP.NET Core
|
||||
|
@ -574,7 +574,7 @@ The preceding constraint is applied in the following code:
|
|||
|
||||
[!code-csharp[](routing/samples/3.x/RoutingSample/Controllers/TestController.cs?name=snippet&highlight=6,13)]
|
||||
|
||||
The [MyDisplayRouteInfo](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/fundamentals/routing/samples/3.x/RoutingSample/Extensions/ControllerContextExtensions.cs) method is included in the [sample download](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/fundamentals/routing/samples/3.x) and is used to display routing information.
|
||||
[!INCLUDE[](~/includes/MyDisplayRouteInfo.md)]
|
||||
|
||||
The implementation of `MyCustomConstraint` prevents `0` being applied to a route parameter:
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace RoutingSample.Controllers
|
||||
{
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace RoutingSample.Controllers
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace RoutingSample.Controllers
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace RoutingSample.Controllers
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace RoutingSample.Controllers
|
||||
{
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
#region snippet
|
||||
internal static class ControllerContextExtensions
|
||||
{
|
||||
public static IActionResult MyDisplayRouteInfo(this ControllerContext ctx, int? id, string msg = null) =>
|
||||
ctx.MyDisplayRouteInfo(id?.ToString(), msg);
|
||||
|
||||
public static IActionResult MyDisplayRouteInfo(this ControllerContext ctx,
|
||||
string id = null, string msg = null)
|
||||
{
|
||||
var actionDescriptor = ctx.ActionDescriptor;
|
||||
var routeTemplate = actionDescriptor?.AttributeRouteInfo?.Template;
|
||||
var routeName = actionDescriptor.AttributeRouteInfo?.Name;
|
||||
var actionName = actionDescriptor.ActionName;
|
||||
var controllerName = actionDescriptor.ControllerName;
|
||||
var routeOrder = actionDescriptor.AttributeRouteInfo?.Order;
|
||||
var method = ctx.HttpContext.Request.Method;
|
||||
|
||||
var tms = (routeTemplate == null) ? "" : $"Template = {routeTemplate}";
|
||||
var ids = (string.IsNullOrEmpty(id)) ? "" : $"id = {id}";
|
||||
var ors = (routeOrder == null) ? "" : $"Order = {routeOrder}";
|
||||
var methods = (method == "GET") ? "" : $"{method}";
|
||||
|
||||
return new ContentResult
|
||||
{
|
||||
Content = $"{methods} {ids} {ors} {tms} {controllerName}.{actionName} {routeName} {msg}"
|
||||
};
|
||||
}
|
||||
}
|
||||
#endregion
|
|
@ -7,6 +7,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
|
||||
<PackageReference Include="Rick.Docs.Samples.RouteInfo" Version="1.0.0*" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
[MyDisplayRouteInfo](https://github.com/Rick-Anderson/RouteInfo/blob/master/Microsoft.Docs.Samples.RouteInfo/ControllerContextExtensions.cs) is provided by the [Rick.Docs.Samples.RouteInfo](https://www.nuget.org/packages/Rick.Docs.Samples.RouteInfo) NuGet package and displays route information.
|
|
@ -45,18 +45,17 @@ The route template `"{controller=Home}/{action=Index}/{id?}"`:
|
|||
* Matches a URL path like `/Products/Details/5`
|
||||
* Extracts the route values `{ controller = Products, action = Details, id = 5 }` by tokenizing the path. The extraction of route values results in a match if the app has a controller named `ProductsController` and a `Details` action:
|
||||
|
||||
[!code-csharp[](routing/samples/3.x/main/Controllers/ProductsController.cs?name=snippetA)]
|
||||
[!code-csharp[](routing/samples/3.x/main/Controllers/ProductsController.cs?name=snippetA)]
|
||||
|
||||
The [MyDisplayRouteInfo](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/mvc/controllers/routing/samples/3.x/main/Extensions/ControllerContextExtensions.cs) method is included in the [sample download](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/mvc/controllers/routing/samples/3.x) and is used to display routing information.
|
||||
[!INCLUDE[](~/includes/MyDisplayRouteInfo.md)]
|
||||
|
||||
* `/Products/Details/5` model binds the value of `id = 5` to set the `id` parameter to `5`. See [Model Binding](xref:mvc/models/model-binding) for more details.
|
||||
* `/Products/Details/5` model binds the value of `id = 5` to set the `id` parameter to `5`. See [Model Binding](xref:mvc/models/model-binding) for more details.
|
||||
* `{controller=Home}` defines `Home` as the default `controller`.
|
||||
* `{action=Index}` defines `Index` as the default `action`.
|
||||
* The `?` character in `{id?}` defines `id` as optional.
|
||||
* Default and optional route parameters don't need to be present in the URL path for a match. See [Route Template Reference](xref:fundamentals/routing#route-template-reference) for a detailed description of route template syntax.
|
||||
* Matches the URL path `/`.
|
||||
* Produces the route values `{ controller = Home, action = Index }`.
|
||||
* The [MyDisplayRouteInfo](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/mvc/controllers/routing/samples/3.x/main/Extensions/ControllerContextExtensions.cs) method is included in the [sample download](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/mvc/controllers/routing/samples/3.x) and is used to display routing information.
|
||||
|
||||
The values for `controller` and `action` make use of the default values. `id` doesn't produce a value since there's no corresponding segment in the URL path. `/` only matches if there exists a `HomeController` and `Index` action:
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
{
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#define Five
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#define First
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace RoutingSample.Controllers
|
||||
{
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
//#define MYDEMO4
|
||||
//define MYDEMO4
|
||||
//#define MYDEMO3
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
// This uses same routes as HomeController, so only one can be defined without setting order
|
||||
// Test with webBuilder.UseStartup<StartupDefaultMVC>();
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#define MYDEMO
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
// This uses same routes as HomeController, and MyDemo3Controller so only one can be defined
|
||||
// Test with webBuilder.UseStartup<StartupAPI>();
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#define PROD1
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
{
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Routing;
|
||||
using System;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
{
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define PROD2
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
{
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
{
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
{
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
|
|
|
@ -3,10 +3,12 @@
|
|||
|
||||
// This is the ultimate greedy route
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
{
|
||||
#region snippet_1
|
||||
#region snippet_2
|
||||
#region snippet_1
|
||||
#region snippet_2
|
||||
public class UrlGeneration2Controller : Controller
|
||||
{
|
||||
[HttpGet("")]
|
||||
|
@ -15,14 +17,14 @@ namespace WebMvcRouting.Controllers
|
|||
var url = Url.RouteUrl("Destination_Route");
|
||||
return ControllerContext.MyDisplayRouteInfo("", $" URL = {url}");
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
[HttpGet("custom/url/to/destination2", Name = "Destination_Route")]
|
||||
public IActionResult Destination()
|
||||
{
|
||||
return ControllerContext.MyDisplayRouteInfo();
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
#region snippet_1
|
||||
public class UrlGenerationAttrController : Controller
|
||||
{
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Docs.Samples;
|
||||
|
||||
namespace WebMvcRouting.Controllers
|
||||
{
|
||||
#region snippet_1
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
#region snippet
|
||||
internal static class ControllerContextExtensions
|
||||
{
|
||||
public static IActionResult MyDisplayRouteInfo(this ControllerContext ctx, int ?id, string msg = null) =>
|
||||
ctx.MyDisplayRouteInfo(id?.ToString(), msg);
|
||||
|
||||
public static IActionResult MyDisplayRouteInfo(this ControllerContext ctx,
|
||||
string id = null, string msg = null)
|
||||
{
|
||||
var actionDescriptor = ctx.ActionDescriptor;
|
||||
var routeTemplate = actionDescriptor?.AttributeRouteInfo?.Template;
|
||||
var routeName = actionDescriptor.AttributeRouteInfo?.Name;
|
||||
var actionName = actionDescriptor.ActionName;
|
||||
var controllerName = actionDescriptor.ControllerName;
|
||||
var routeOrder = actionDescriptor.AttributeRouteInfo?.Order;
|
||||
var method = ctx.HttpContext.Request.Method;
|
||||
|
||||
var tms = (routeTemplate == null) ? "" : $"Template = {routeTemplate}";
|
||||
var ids = (string.IsNullOrEmpty(id)) ? "" : $"id = {id}";
|
||||
var ors = (routeOrder == null) ? "" : $"Order = {routeOrder}";
|
||||
var methods = (method == "GET") ? "" : $"{method}";
|
||||
|
||||
return new ContentResult {
|
||||
Content = $"{methods} {ids} {ors} {tms} {controllerName}.{actionName} {routeName} {msg}" };
|
||||
}
|
||||
}
|
||||
#endregion
|
|
@ -36,7 +36,7 @@
|
|||
<a href-key="Products13 Index"></a>
|
||||
</li>
|
||||
<li>
|
||||
<a href-key="MyTestApi"></a> ZZ
|
||||
<a href-key="MyTestApi"></a>
|
||||
<a href-key="MyTestApiController"></a>
|
||||
</li>
|
||||
<li>
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
|
||||
<PackageReference Include="Rick.Docs.Samples.RouteInfo" Version="1.0.*" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Reference in New Issue