web api控制器中的多个帖子 [英] multiple post in web api controller
问题描述
我有一个web api控制器和一个针对我的js文件的knockout.js页面。我的api控制器需要两个post方法。现在我的代码是这样的:
在knockout.js
self。 addTicket = function(){
$ .ajax({
url:api / helpdeskapi / Post,
type:'post',
data:ko.toJSON(this ),
contentType:'application / json',
success:function(result){}
});
$('#AddNewTicketpopup')。对话框('关闭')
self.addCategory = function(){
$ .ajax({
url:api / helpdeskapi / AddCategory,
type:'post',
data:ko.toJSON(this),
contentType:'application / json',
success:function(result){}
});
在api控制器中:
< pre lang =xml> public HttpResponseMessage Post(TKTJOB job)
{
HelpDeskRepository.AddJob(job);
var response = Request.CreateResponse < TKTJOB > (HttpStatusCode.Created,job);
string uri = Url.Link(DefaultApi,new {id = job.JobId});
返回回复;
}
public HttpResponseMessage AddCategory(JOBCATEGORY类别) )
{
HelpDeskRepository.AddCategory(category);
var response = Request.CreateResponse < JOBCATEGORY > (HttpStatusCode.Created,类别);
string uri = Url.Link(DefaultApi,new {id = category.CategoryId});
返回回复;
}
当我运行这个我只能发布票价值(这只是第一个帖子方法正在运行)。第二个不工作。
任何人都可以帮我这样做。
.ajax({
url:api / helpdeskapi / Post,
type:'post',
data:ko.toJSON(this),
contentType:'application / json',
success:function(result){}
});
('#AddNewTicketpopup')。dialog('close' )
self.addCategory = function(){
.ajax({
url:api / helpdeskapi / AddCategory,
类型:'post',
数据:ko.toJSON(this),
contentType:'application / json',
成功:函数(结果){}
});
在api控制器中:
public HttpResponseMessage P ost(TKTJOB job)
{
HelpDeskRepository.AddJob(job);
var response = Request.CreateResponse < TKTJOB > (HttpStatusCode.Created,job);
string uri = Url.Link(DefaultApi,new {id = job.JobId});
返回回复;
}
public HttpResponseMessage AddCategory(JOBCATEGORY类别) )
{
HelpDeskRepository.AddCategory(category);
var response = Request.CreateResponse < JOBCATEGORY > (HttpStatusCode.Created,类别);
string uri = Url.Link(DefaultApi,new {id = category.CategoryId});
返回回复;
}
当我运行这个我只能发布票价值(这只是第一个帖子方法正在运行)。第二个不工作。
任何人都可以帮我这样做。
Hi,
I have a web api controller and a knockout.js page for my js files. i need two post methods in my api controller. Now my code is this:
In knockout.js
self.addTicket = function () { $.ajax({ url: "api/helpdeskapi/Post", type: 'post', data: ko.toJSON(this), contentType: 'application/json', success: function (result) { } }); $('#AddNewTicketpopup').dialog('close')
self.addCategory = function () { $.ajax({ url: "api/helpdeskapi/AddCategory", type: 'post', data: ko.toJSON(this), contentType: 'application/json', success: function (result) { } });
In api controller:
public HttpResponseMessage Post(TKTJOB job)
{
HelpDeskRepository.AddJob(job);
var response = Request.CreateResponse<TKTJOB>(HttpStatusCode.Created, job);
string uri = Url.Link("DefaultApi", new { id = job.JobId });
return response;
}
public HttpResponseMessage AddCategory(JOBCATEGORY category)
{
HelpDeskRepository.AddCategory(category);
var response = Request.CreateResponse<JOBCATEGORY>(HttpStatusCode.Created, category);
string uri = Url.Link("DefaultApi", new { id = category.CategoryId });
return response;
}
when i run this i can post only the ticket values(that is only the first post method is working). Second one is not working.
Can anyone help me in doing this.
.ajax({ url: "api/helpdeskapi/Post", type: 'post', data: ko.toJSON(this), contentType: 'application/json', success: function (result) { } });
('#AddNewTicketpopup').dialog('close')
self.addCategory = function () {
.ajax({ url: "api/helpdeskapi/AddCategory", type: 'post', data: ko.toJSON(this), contentType: 'application/json', success: function (result) { } });
In api controller:
public HttpResponseMessage Post(TKTJOB job) { HelpDeskRepository.AddJob(job); var response = Request.CreateResponse<TKTJOB>(HttpStatusCode.Created, job); string uri = Url.Link("DefaultApi", new { id = job.JobId }); return response; }
public HttpResponseMessage AddCategory(JOBCATEGORY category) { HelpDeskRepository.AddCategory(category); var response = Request.CreateResponse<JOBCATEGORY>(HttpStatusCode.Created, category); string uri = Url.Link("DefaultApi", new { id = category.CategoryId }); return response; }
when i run this i can post only the ticket values(that is only the first post method is working). Second one is not working.
Can anyone help me in doing this.
这篇关于web api控制器中的多个帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!