简体   繁体   English

使用ASP Net Core修改Web API中传入请求的路由

[英]Modify route of incoming request in Web API using ASP Net Core

I am working on a Web API project using Asp Net Core 2.1. 我正在使用Asp Net Core 2.1进行Web API项目。 My existing Web API has the following Get method (and numerous other methods like these): 我现有的Web API具有以下Get方法(以及许多其他类似方法):

[HttpGet("GetEmployeByName/{Name}")]
public Employee GetEmployeeByName(string name)
{... some code...}

This responds to the following incoming request: 这将响应以下传入请求:

http://localhost:8080/Employee/GetEmployeeByName/{Name}

Now my requirement is such that some of the APIs could be called by passing an additional id such as: 现在,我的要求是可以通过传递其他ID来调用某些API,例如:

http://localhost:8080/Employee/{Id}/GetEmployeeByName/{Name}.

I still need to map it to the original method without changing the route. 我仍然需要将其映射到原始方法而不更改路由。 This can be done using ASP Net Core's feature of URL Rewrite and the URL can be re-written without the id. 可以使用ASP Net Core的URL重写功能来完成此操作,并且可以在没有id的情况下重写URL。

However what i want to do is to capture the {Id} as well and save it to Session\\ Context. 但是,我想做的是也捕获{Id}并将其保存到Session \\ Context。

Does anyone know how to extract the id and rewrite the URL. 有谁知道如何提取ID并重写URL。

也许您可以在此处找到所需的可选参数web api ,您需要在最后添加该参数,因为可选参数总是在最后使用,也可以尝试使用两种版本的端点封装逻辑并重用它在两个版本上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM