Asp.net mvc .net web api Http post方法

Asp.net mvc .net web api Http post方法,asp.net-mvc,Asp.net Mvc,我对.NET Web Api没有太多经验,但我一直在为一个课程项目使用它。该应用程序运行良好,但我现在正在努力通过fidler测试POST控制器。 给我一个如何测试wep api http post方法的指南对我来说唯一的问题是测试post方法。 我最近看了很多文章并在上面应用了它,但它们从来都不起作用 任何帮助都将非常感激: 我的控制器看起来像: public class DonationCausesController : Controller { // // GET: /D

我对.NET Web Api没有太多经验,但我一直在为一个课程项目使用它。该应用程序运行良好,但我现在正在努力通过fidler测试POST控制器。 给我一个如何测试wep api http post方法的指南对我来说唯一的问题是测试post方法。 我最近看了很多文章并在上面应用了它,但它们从来都不起作用

任何帮助都将非常感激:

我的控制器看起来像:

public class DonationCausesController : Controller
{
    //
    // GET: /DonationCauses/

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public void addcauses(Causes causes) 
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = @"Data Source=Lenovo-PC\SQLEXPRESS;Initial Catalog=Donation_db;Integrated Security=True";  
        //SqlCommand sqlCmd = new SqlCommand("INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)", myConnection);  
        string approving_status="not approved";
        SqlCommand sqlCmd = new SqlCommand();  
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "INSERT INTO Volunteer_problem(Volunteer_name,Volunteer_cause,Cause_location,Volunteer_contact,Approved) Values (@Volunteer_name,@Volunteer_cause,@Cause_location,@Volunteer_contact,@Approved)";  
        sqlCmd.Connection = myConnection;  


        sqlCmd.Parameters.AddWithValue("@Volunteer_name", causes.volunteer_name);  
        sqlCmd.Parameters.AddWithValue("@Volunteer_cause", causes.volunteer_cause);  
        sqlCmd.Parameters.AddWithValue("@Cause_location", causes.cause_location);  
        sqlCmd.Parameters.AddWithValue("@Volunteer_contact", causes.volunteer_contact);
        sqlCmd.Parameters.AddWithValue("@Approved", approving_status);  
        myConnection.Open();  
        int rowInserted = sqlCmd.ExecuteNonQuery();  
        myConnection.Close();
    }
}
属性类别:

public class Causes
{
    public string volunteer_name { get; set; }
    public string volunteer_cause { get; set; }
    public string cause_location { get; set; }
    public int volunteer_contact { get; set; }
}

在fiddler的请求生成器中,您有两个部分:请求头和请求体。在
Get
请求中,body显然是空的。但是在
Post
Put
中,可以指定正文的内容。对于您的具体问题,您可以通过以下两种方式之一实现目标
1-在请求头的
内容类型
头中使用Json。如果您采用此路线,您的请求头将类似

User-Agent: Fiddler
Authorization: your auth token if using one
Host: localhost:61026
Content-Length: 788
Content-Type: application/json
User-Agent: Fiddler
    Authorization: your auth token if using one
    Host: localhost:61026
    Content-Length: 788
    Content-Type: application/x-www-form-urlencoded
请求体应该是这样的

{"volunteer_name": "good name", "volunteer_cause": "good cause", 
"cause_location" :"my location", "volunteer_contact" : 5}
2-如果您使用
应用程序/x-www-form-urlencoded
作为
内容类型
。您的标题看起来像

User-Agent: Fiddler
Authorization: your auth token if using one
Host: localhost:61026
Content-Length: 788
Content-Type: application/json
User-Agent: Fiddler
    Authorization: your auth token if using one
    Host: localhost:61026
    Content-Length: 788
    Content-Type: application/x-www-form-urlencoded
你的请求主体看起来像

volunteer_name=good name&volunteer_cause=good_cause&volunteer_location=my location&volunteer_contact=5   

请注意,您不需要在请求中指定
内容长度
标题,它将由fiddler在fiddler的请求生成器中自动计算,您有两个部分:请求标题和请求正文。在
Get
请求中,body显然是空的。但是在
Post
Put
中,可以指定正文的内容。对于您的具体问题,您可以通过以下两种方式之一实现目标
1-在请求头的
内容类型
头中使用Json。如果您采用此路线,您的请求头将类似

User-Agent: Fiddler
Authorization: your auth token if using one
Host: localhost:61026
Content-Length: 788
Content-Type: application/json
User-Agent: Fiddler
    Authorization: your auth token if using one
    Host: localhost:61026
    Content-Length: 788
    Content-Type: application/x-www-form-urlencoded
请求体应该是这样的

{"volunteer_name": "good name", "volunteer_cause": "good cause", 
"cause_location" :"my location", "volunteer_contact" : 5}
2-如果您使用
应用程序/x-www-form-urlencoded
作为
内容类型
。您的标题看起来像

User-Agent: Fiddler
Authorization: your auth token if using one
Host: localhost:61026
Content-Length: 788
Content-Type: application/json
User-Agent: Fiddler
    Authorization: your auth token if using one
    Host: localhost:61026
    Content-Length: 788
    Content-Type: application/x-www-form-urlencoded
你的请求主体看起来像

volunteer_name=good name&volunteer_cause=good_cause&volunteer_location=my location&volunteer_contact=5   

请注意,您不需要在请求中指定
内容长度
标题,它将由fiddler自动计算

试用Chrome PostMan。试试Chrome的邮递员。兄弟,我写的和你描述的一样,但仍然没有回应。。。!HTTP/1.1 404未找到当我按下执行按钮时,将显示此消息详细信息:{“消息”:“未找到与请求URI匹配的HTTP资源”未找到与名为“DonationCauss”的控制器匹配的类型。}您的路由可能有问题,顺便说一句,您需要从
ApiController
继承来创建web api。从您的代码来看,您似乎继承了asp.net mvc范例中的
Controller
,我编写的代码与您描述的相同,但仍然没有响应。。。!HTTP/1.1 404未找到当我按下执行按钮时,将显示此消息详细信息:{“消息”:“未找到与请求URI匹配的HTTP资源”未找到与名为“DonationCauss”的控制器匹配的类型。}您的路由可能有问题,顺便说一句,您需要从
ApiController
继承来创建web api。从您的代码中,您似乎继承了asp.net mvc范例中的
Controller