编码电子邮件传递给Web.API [英] Encode Email to pass to Web.API

查看:113
本文介绍了编码电子邮件传递给Web.API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送电子邮件地址到 Web.Api获取方法。我允许有人注册一个新帐户之前,我需要查看邮件地址是否存在于我们的系统中。

I am needing to send an email address to a Web.Api get method. I need to check to see if the email address exists in our system before allowing someone to sign up for a new account.

我的第一个想法是编码电子邮件地址,然后解码一旦它在 Get Action

My first thought was to encode the email address and then decode it once it was inside the Get Action

网址看起来像这样
http://mysite/Api/RTSCredit/jharding%40email.com

但是,总是出现错误404回复

However, This always errors with a 404 response

这里是 WebApiConfig.cs

 config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{emailAddress}",
      defaults: new { id = RouteParameter.Optional }
  );

当我发送一个简单的字符串 http:// mysite / Api / RTSCredit / someemail

When I send in a simple string http://mysite/Api/RTSCredit/someemail

它像我喜欢的那样获取Get操作,但显然不能返回有用的值。

It hits the Get action like I would prefer, but obviously can't return a useful value.

这是Get Action

Here is the Get Action

[HttpGet]
public HttpResponseMessage Get([FromUri] string emailAddress)
{
    using (IRtsCreditDal rtsCreditDal = new RtsCreditDal())
    {
        var listOfExistingUsers = rtsCreditDal.GetUsersByEmail(emailAddress);
        if (listOfExistingUsers == null || listOfExistingUsers.Count == 0)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound, "not found");
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.OK); 
        }
    }
  }

我需要做些什么调整为了传递电子邮件到这个 Action

What adjustments do I need to make in order to pass an email to this Action?

当我进一步玩,似乎不喜欢期间 URL。我可以很容易地做某些事来替代这个角色,但是感觉就像是一个黑客。

As I've been playing further, it doesn't seem to like the period in the URL. I could easily do something to replace that character but it feels like a hack.

推荐答案

在从客户端到达的电子邮件结尾处:

Try adding a trailing / at the end of the email reaching from the client:

http://localhost:8800/api/users/user@gmail.com/

这篇关于编码电子邮件传递给Web.API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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