简体   繁体   English

Web Api 给出错误:500 内部服务器错误

[英]Web Api giving error: 500 internal server error

Scenario:设想:

I have an ASP WebAPI service that accept email id to retrieve the previous password.我有一个 ASP WebAPI 服务,它接受email id来检索以前的密码。

But I am having a problem that If I enter a valid email Id "Internal server error 500" is thrown but if a wrong email Id is entered, then Json with error message is returned.但是我遇到了一个问题,如果我输入了一个有效的email Id "Internal server error 500" ,但如果输入了错误的email Id ,则返回带有错误消息的 Json。 (If an invalid email id is entered, then I have a Json with error message returned) (如果输入了无效的email id ,那么我将返回一个带有错误消息的 Json)

So can please any one guide what to do.所以可以请任何人指导如何做。

var urll = 'api/BeerAppRetrieve';
   // var urll = 'api/BeerApp';
    var emailId = "anirudh1190@gmail.com";
    //$.getJSON(urll + '/' + data)
    $.getJSON(urll, { "emailId": emailId })
       .done(function (data) {
       })
       .fail(function (jqXHR, textStatus, err) {

           alert("error" + jqXHR.responseText);
           $('#txtUserName').text('Error: ' + err);
       });

My json call.. I want the method to be hit if email id is valid also.. Please help...我的 json 调用 .. 如果电子邮件 ID 也有效,我希望该方法被命中.. 请帮助...

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

My route config.我的路由配置。

  [EnableCors("*", "*", "GET, POST")]
  public IHttpActionResult GetForgotPassword(String emailId)
  {




MailMessage MyMailMessage = new MailMessage();
        MyMailMessage.From = new MailAddress("beersavo@gmail.com");
        MyMailMessage.To.Add(emailId);
        MyMailMessage.Subject = "Forgot Password";
        MyMailMessage.IsBodyHtml = true;
        BeerAppUserClass beerAppDal = new BeerAppUserClass();
        String passwrd = String.Empty;
        try
        {
            passwrd = beerAppDal.GetUserPassword(emailId);
        }

catch (Exception exw)
{
    return Json(new KeyValuePair<String, String>("MailSend", exw.InnerException.Message));
}

if (!passwrd.Equals(String.Empty))
{


MyMailMessage.Body = "<table><tr><td>" + "Login to the site with:</td></tr> <tr><td>  UserName: </td><td>" + emailId + "</td></tr> <tr><td> Password:</td><td>" + passwrd + "</td></tr></table>";
        SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
        SMTPServer.Port = 587;
        //SMTPServer.Host = "smtpout.secureserver.net";
        SMTPServer.Credentials = new System.Net.NetworkCredential("email@gmail.com", "Password@321");
        SMTPServer.EnableSsl = true;
        try
        {
            SMTPServer.Send(MyMailMessage);
            return Json(new KeyValuePair<String, String>("MailSend", "true"));
        }

        catch (Exception ex)
        {


return Json(newKeyValuePair<String,String("MailSend",ex.InnerException.Message));
        }
    }
    return Json(new KeyValuePair<String, String>("MailSend", "empty"));
}
  }

Action in the controller控制器中的动作

Perhaps more of a comment.. .也许更多的评论......

though the issue is with having the email in the route - it has a period .尽管问题在于在路由中包含电子邮件 - 它有一个句点. - this is throwing off your routing. - 这正在抛弃你的路由。 i had the exact same issue on a previous project that had email addresses as ID's我在以前的项目中遇到了完全相同的问题,该项目的电子邮件地址为 ID

I would wager that when you try it with a bad email the bad string you're trying with doesn't have a period.我敢打赌,当您尝试使用糟糕的电子邮件时,您尝试使用的错误字符串没有句点。

Use a querystring, so 'api/BeerAppRetrieve?theemail=theactual@email.com`使用查询字符串,所以 'api/BeerAppRetrieve?theemail=theactual@email.com`

or change your route config to accept the period或更改您的路线配置以接受期间

or you can replace certain characters in the email client side and change them back server side (worst case)或者您可以替换电子邮件客户端中的某些字符并将它们更改回服务器端(最坏的情况)

As a super worst case you could add the following in your web.config (you should really avoid this otheriwse EVERY request on your site goes through the asp.net pipeline..作为最糟糕的情况,您可以在 web.config 中添加以下内容(您应该真正避免这种情况)您网站上的每个请求都通过 asp.net 管道..

<configuration>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"> <!-- add this -->

Update更新

In your Javascript change:在您的 Javascript 更改中:

var urll = 'api/BeerAppRetrieve';

to this对此

var urll = 'api/BeerAppRetrieve?emailAddress=anirudh1190@gmail.com';

And change your route back to并将您的路线改回

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

and change your API controller to并将您的 API 控制器更改为

[EnableCors("*", "*", "GET, POST")]
  public IHttpActionResult GetForgotPassword(String id)
  {
    //my code
  }

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

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