简体   繁体   中英

How can I test my web api Post Web method to know what's going on inside?

So here's my situation...

We have an on-prem installation of Microsoft Dynamics CRM and I am trying to make an ajax call from it to a service I created on another one of our servers. There have been many issues already that I've solved - but I'm able at this point to successfully make a GET request to my service from CRM via javascript I've put on a form in CRM.

Just for reference (because I'm not entirely sure at this point if these things are related or not)...

  • I had to set anonymous authentication in IIS for my service (CRM has its own authentication that I will be relying on)
  • I had to set a response header of Access-Control-Allow-Origin with the host address of our CRM installation

So, after doing those things I was able to successfully call my web service via GET. I could return back a string I had from a [HttpGet] web method.

But, now I need to actually call a web method via POST to post some data to my web service. So, below you can see my implementation for the service as well as the javascript I'm using the make the POST call.

using CRMService.Models;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Http;

namespace CRMService.Controllers
{
    public class DefaultController : ApiController
    {
        // GET: Default
        public string Get()
        {
            return "Hi";
        }

        [HttpPost]
        public string GiveParameters(TestClass tc)
        {
            try
            {
                Dictionary<string, object> Parameters = new Dictionary<string, object>();
                Parameters.Add("id", tc.id);
                Parameters.Add("one", tc.one);
                Parameters.Add("two", tc.two);
                NonQuery("InsertTestItem", ConfigurationManager.ConnectionStrings["TestConnection"].ToString(), Parameters);
                return "success";
            }
            catch (Exception ex)
            {
                return "ex";
            }
        }               
    }
}


var new_budget = Xrm.Page.data.entity.attributes.get("new_budget").getValue();
var new_name = Xrm.Page.data.entity.attributes.get("new_name").getValue();
var id = Xrm.Page.data.entity.getId();
data = '{"TestClass":{"one":"' + new_name + '", "two":"'+ new_budget +'", "id":"'+ id +'"}}'

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "https://<hiddingMyUrl>/api/Default/GiveParameters",
    data: data,
    dataType: "json",
    success: function(data) {
        console.log("Success");
    },
    error: function(result) {
        console.log("Error");
    }
});   

When I make this POST call, at first I could see it was doing some "preflight" stuff and making an OPTIONS request - then returning a 403 (I think, if memory serves me right). I looked that up and solved that issue by adding a Access-Control-Allow-Headers header to my web service in IIS with the value of Origin, X-Requested-With, Content-Type, Accept

After doing that my POST actually gives a 200 status code - but, as you can see in my code, I should then be seeing data in a database if everything went well.
..So of course then the question is... is my web service code working properly? And normally I could test for that easily - however I am fairly new to web api. I don't really get the best way to testing at this point - and I don't know if it's something with my code specifically or if there is some configuration issue with web api itself.

Here is my routeconfig:

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { action = "Index", id = UrlParameter.Optional }
        );
    }

You should try working with a REST client. Here are two nice ones :

I personally prefer Postman but really both are good.

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