我有一个ApiController,我想使用电子邮件地址作为请求的ID参数:
// GET api/employees/email@address.com
public CompactEmployee Get(string id) {
var email = id;
return GetEmployeeByEmail(email);
}
Run Code Online (Sandbox Code Playgroud)
但是,我不能让它工作(返回404):
http://localhost:1080/api/employees/employee@company.com
以下所有工作:
http://localhost:1080/api/employees/employee@companyhttp://localhost:1080/api/employees/employee@company.http://localhost:1080/api/employees?id=employee@company.com我已经设置relaxedUrlToFileSystemMapping="true"了我的web.config,详见Phil Haack.
我非常喜欢完整的电子邮件地址,但是任何时候任何其他角色跟着这个时期,请求都会返回404.任何帮助都将非常感谢!
由于缺乏其他选项,我已朝着Maggie建议的方向前进,并使用此问题的答案来创建重写规则,以便在我需要URL中的电子邮件时自动附加尾部斜杠.
<system.webServer>
....
<rewrite>
<rules>
<rule name="Add trailing slash" stopProcessing="true">
<match url="^(api/employees/.*\.[a-z]{2,4})$" />
<action type="Rewrite" url="{R:1}/" />
</rule>
</rules>
</rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud) 我试图在我的ASP.net MVC4/WebApi项目中使用以下(和类似的)URL:
http://127.0.0.1:81/api/nav/SpotiFire/SpotiFire.dll
Run Code Online (Sandbox Code Playgroud)
负责此网址的路线如下所示:
config.Routes.MapHttpRoute(
name: "Nav",
routeTemplate: "api/nav/{project}/{assembly}/{namespace}/{type}/{member}",
defaults: new { controller = "Nav", assembly = RouteParameter.Optional, @namespace = RouteParameter.Optional, type = RouteParameter.Optional, member = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
如果我删除它,它的工作正常.在文件名中,或者如果我在URL后面添加斜杠,但这也意味着我不能使用Url.Route-methods等.我得到的错误是一般的404错误(下图).

我已经尝试添加<httpRuntime targetFramework="4.5" relaxedUrlToFileSystemMapping="true" />到我的web.config,我也尝试添加
<compilation debug="true" targetFramework="4.5">
<buildProviders>
<remove extension=".dll"/>
<remove extension=".exe"/>
</buildProviders>
</compilation>
Run Code Online (Sandbox Code Playgroud)
而且似乎没有任何效果.所以我的问题基本上是,如何让这个URL工作,并正确映射?
如何在ASP.NET MVC Web API的URL末尾将电子邮件作为参数传递?
如下:
test.com/api/sales/getcustomerorders/test@test.com
Run Code Online (Sandbox Code Playgroud)
我想将电子邮件地址作为参数传递给getcustomerorders操作.
我们可以使用查询字符串传递.但我想像上面那样格式化网址.
谢谢.