admin管理员组

文章数量:1429550

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 = "[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...

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");
        SMTPServer.Port = 587;
        //SMTPServer.Host = "smtpout.secureserver";
        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

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 = "[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...

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.");
        SMTPServer.Port = 587;
        //SMTPServer.Host = "smtpout.secureserver";
        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

Share Improve this question edited Dec 1, 2023 at 6:11 Amit Mohanty 1,8782 gold badges15 silver badges23 bronze badges asked Aug 22, 2014 at 17:23 Rohit PaulRohit Paul 3361 gold badge3 silver badges13 bronze badges 5
  • Have you tried debugging the server-side code? – Matthew Commented Aug 22, 2014 at 17:25
  • 2 Internal Server Error ... Start by posting the server code – Jonesopolis Commented Aug 22, 2014 at 17:25
  • Let's see your WebApi Controller/Method/Action. Most people mix up MVC and WebApi routing – Brunis Commented Aug 22, 2014 at 17:25
  • Internal Server Error is about server side error. So you must show us your server side code. – Bart Calixto Commented Aug 22, 2014 at 17:28
  • please show //my code ... – Bart Calixto Commented Aug 22, 2014 at 17:37
Add a ment  | 

1 Answer 1

Reset to default 0

Perhaps more of a ment.. .

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/[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 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 pipeline..

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

Update

In your Javascript change:

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

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

本文标签: cWeb Api giving error 500 internal server errorStack Overflow