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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25452449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:31:43  来源:igfitidea点击:

Web Api giving error: 500 internal server error

c#javascript.netjsonasp.net-web-api

提问by Rohit Paul

Scenario:

设想:

I have an ASP WebAPI service that accept emailidto retrieve the previous password.

我有一个 ASP WebAPI 服务,它接受emailid检索以前的密码。

But I am having a problem that If I enter a valid emailId"Internal server error 500"is thrown but if a wrong emailIdis entered, then Json with error message is returned. (If an invalid emailidis entered, then I have a Json with error message returned)

但是我遇到了一个问题,如果我输入了一个有效的emailId"Internal server error 500",但如果输入了一个错误emailId,则返回带有错误消息的 Json。(如果emailid输入无效,那么我有一个返回错误消息的 Json)

So can please any one guide what to do.

所以可以请任何人指导如何做。

var urll = 'api/BeerAppRetrieve';
   // var urll = 'api/BeerApp';
    var emailId = "[email protected]";
    //$.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("[email protected]");
        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 protected]", "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

控制器中的动作

回答by Darren Wainwright

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/[email protected]`

使用查询字符串,所以 'api/[email protected]`

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 caseyou 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/[email protected]';

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
  }