ASP.Net WebAPI2 debugging from ASP.Net MVC4 application

I have an ASP.Net WebAPI2 application that has functionality in it that is required by my ASP.Net MVC application.

When I try to call the WebAPI2 function URL from the MVC application, I get a 401 Unauthorized error.

One is running on localhost:49494 (WebAPI2) and the other is running on localhost:50909
I AM able to get results from calling the api function from the address bar AND by calling from AJAX.

How can I get these two to talk to each other so that I may test this interaction from my MVC application?

The WebAPI2 app will be deployed to a production server as will the MVC app after testing is done.

This is the GetERData controller function from my WebAPI project:

[HttpGet]
[Route("api/ER/GetERData/{PID}/{ECode}")]
public IHttpActionResult GetERData(int PID, string ECode)
{
    string msg = String.Concat("api/ER/GetERData", "/", PID.ToString(), "/", ECode);
    try
    {
        var result = _ERRepository.GetERData(PID, ECode);
        return Ok(result);
    }
    catch (Exception ex)
    {
        /* commenting out during testing
        log.WriteToEventLog(msg, "error");
        log.WriteToEventLog(ex.Message, "error");
        */
        throw ex;
    }
}

This is the function in the repository that retrieves the data and loads the model:
Utilizing EF6 connecting to Oracle.

public ERModel.ERData GetERData(int PID, string ECode)
{
    ERModel.ERData res = new ERModel.ERData();
    ERModel.ER erd = new ERModel.ER();
    erd = GetData(PID, ECode);
    res.Address = erd.Address;
    res.bus_phone = erd.bus_phone;
    res.cell_phone = erd.cell_phone;
    res.company = erd.company;
    res.email = erd.email;
    res.full_name = erd.full_name;
    res.pass_ratio = erd.pass_ratio;
    res.score = erd.score;
    res.title = erd.title;
    res.CHistory = GetCHistory(PID);
    return res;
}

and the GetData function:

private ERModel.ER GetERData(int PID, string ECode)
{
    var sql = "select...."; // PL/SQL code with parameters filled 
    var res = _Db.Database.SqlQuery<ERModel.ER>(sql); 
    return res.FirstOrDefault();
}

and the GetCHistory function:

private List<ERModel.CHistory> GetCHistory(int PID)
{
    var sql = "select...."; // PL/SQL code with parameters filled
    var res = _Db.Database.SqlQuery<ERModel.CHistory>(sql
    return res.ToList();
}

This is the method from the MVC application where it is called:

public ActionResult RequestER(string ECode)
{
    string URL = String.Concat("http://localhost:49494/api/ER/GetERData/", PID, "/", ECode);
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
    request.Method = "GET";
    String data;
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        data = reader.ReadToEnd();
        reader.Close();
        dataStream.Close();
     }

    ERModel.ERData erm = JsonConvert.DeserializeObject<ERModel.ERData>(data);
    erm.ecode = ECode;
    erm.pid = PID.ToString();
    
    MailMessage message = new MailMessage();

    message.From = new MailAddress("all@oursite.com", "TNA");

    message.IsBodyHtml = true;
    message.BodyEncoding = System.Text.Encoding.UTF8;
    message.Subject = "ER";

    MailAddress to = new MailAddress("es@oursite.com");
    message.To.Add(to);

    message.Body = Utilities.RenderPartialViewToString(this, "_EREmailForm", erm);

    SmtpClient client = new SmtpClient();
    client.Send(message);

    return RedirectToAction("Courses", "Home");
}

Hi

To debug more than 1 application at a time, you need to set visual studio to do that as described here

**To set multiple startup projects**
  • In the Solution
  • Explorer, select the solution (the very top node). Right-click the node to get the context menu.
  • Select Properties. The Solution Property Pages dialog box opens.
  • Expand the Common Properties node, and click Startup Project.
  • Click Multiple Startup Projects and set the appropriatet actions. For more information, see How to: Modify Project Properties and Configuration Settings.

2 different projects in 2 different solutions, one in a VM and the other on the computer hosting the VM.

Now, explain how to debug that one…

In that case you need to find the process id of worker process hosting that project. Then from visual studio, you need to attach the debugger to the process via it’s id. Quite a bit trickier to do

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.