ApiController返回404时,ID包含周期 [英] ApiController returns 404 when ID contains period

查看:154
本文介绍了ApiController返回404时,ID包含周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ApiController,我想使用的电子邮件地址作为请求的ID参数:

  //获取api/employees/email@address.com
公共CompactEmployee获取(字符串ID){
   VAR电子邮件= ID;
   返回GetEmployeeByEmail(电子邮件);
}

不过,我不能得到这个工作(返回 404

的http://本地主机:1080/api/employees/employee@company.com

以下各项工作:

的http://本地主机:1080 / API /员工/员工@公司
的http://本地主机:1080 / API /员工/员工@公司
的http://本地主机:1080 / API /员工id=employee@company.com

我已经设置 relaxedUrlToFileSystemMapping =真正的在我的web.config作为的 /allowing-reserved-filenames-in-URLs.aspx\">detailed。

我就非常喜欢完整的电子邮件地址的工作,但期间后跟任何其他字符的任何时间,请求返回一个404的任何帮助将不胜AP preciated!

解决方案

由于缺乏其他选择,我在这个方向前进玛姬建议,并从使用的回答这个问题创建一个重写规则,当我需要在URL的电子邮件自动追加尾部斜杠。

 < system.webServer>
  ....
  <&改写GT;
    <规则与GT;
      <规则名称=添加斜线stopProcessing =真>
        <匹配URL =^(API /员工/.* \\ [A-Z] {2,4})$/>
        <作用TYPE =重写URL ={R:1} //>
      < /规则>
    < /规则>
  < /重写>
< /system.webServer>


会增加一个尾随斜线工作您的方案?

 的http://本地主机:33021/api/employees/employee@company.com/

I have an ApiController and I want to use email addresses as the ID parameter for requests:

// GET api/employees/email@address.com
public CompactEmployee Get(string id) {
   var email = id;
   return GetEmployeeByEmail(email);
}

However, I cannot get this to work (returns 404):

http://localhost:1080/api/employees/employee@company.com

The following all work:

http://localhost:1080/api/employees/employee@company http://localhost:1080/api/employees/employee@company. http://localhost:1080/api/employees?id=employee@company.com

I have set relaxedUrlToFileSystemMapping="true" in my web.config as detailed by Phil Haack.

I would very much love the full email address to work, but any time the period is followed by any other character, the request returns a 404. Any help would be greatly appreciated!

Solution

Due to a lack of other options, I've headed in the direction Maggie suggested and used the answer from this question to create a rewrite rule to automatically append a trailing slash when I need an email in the 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>

解决方案

Would adding a trailing slash work for your scenario?

http://localhost:33021/api/employees/employee@company.com/

这篇关于ApiController返回404时,ID包含周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆