简体   繁体   中英

Web Api giving error: 500 internal server error

Scenario:

I have an ASP WebAPI service that accept email id to retrieve the previous password.

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. (If an invalid email id is entered, then I have a Json with error message returned)

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...

 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

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`

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..

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

Update

In your Javascript change:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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