假设您在创建新的MVC(5)项目时忘记勾选Web API复选框(将其添加到项目中),您需要做什么才能添加Web API并使其正常工作?
有一堆迁移问题,但似乎都没有将Web API添加到MVC 5项目的完整和最新步骤,而且似乎已经从一些旧的答案中改变了.
为什么ASP.NET Web API需要FromBody和FromUri属性?
使用属性和不使用它们之间有什么区别?
我试图在WebAPI控制器上发布多个参数.一个参数来自URL,另一个参数来自正文.这是网址:
/offers/40D5E19D-0CD5-4FBD-92F8-43FDBB475333/prices/
这是我的控制器代码:
public HttpResponseMessage Put(Guid offerId, OfferPriceParameters offerPriceParameters)
{
//What!?
var ser = new DataContractJsonSerializer(typeof(OfferPriceParameters));
HttpContext.Current.Request.InputStream.Position = 0;
var what = ser.ReadObject(HttpContext.Current.Request.InputStream);
return new HttpResponseMessage(HttpStatusCode.Created);
}
Run Code Online (Sandbox Code Playgroud)
正文的内容是JSON:
{
"Associations":
{
"list": [
{
"FromEntityId":"276774bb-9bd9-4bbd-a7e7-6ed3d69f196f",
"ToEntityId":"ed0d2616-f707-446b-9e40-b77b94fb7d2b",
"Types":
{
"list":[
{
"BillingCommitment":5,
"BillingCycle":5,
"Prices":
{
"list":[
{
"CurrencyId":"274d24c9-7d0b-40ea-a936-e800d74ead53",
"RecurringFee":4,
"SetupFee":5
}]
}
}]
}
}]
}
}
Run Code Online (Sandbox Code Playgroud)
知道为什么默认绑定无法绑定到offerPriceParameters我的控制器的参数?它始终设置为null.但是我能够使用the从身体恢复数据DataContractJsonSerializer.
我也尝试使用FromBody参数的属性,但它也不起作用.
我正在使用Asp.net Web Api http://xyzdomain.com:16845/api/returns/returns
我有以下数据,
我如何使用postman chrome extension发布结束点,
鉴于"Items"是一个集合
[
{
"Items": [
{
"sku": "9257",
"Price": "100",
"Quantity": "500",
"DiscountPercent": "1",
"backordered": "2"
}
],
"order_id": "F429768865001",
"status_code": "Shelf",
"Exception": "no error"
}
]
Run Code Online (Sandbox Code Playgroud) 我正在构建一个客户端脚本很重的ASP.NET MVC应用程序,它将使用JSON和jQuery来操作DOM.
我的理解是Web API Controller和MVC Controller都可以返回JSON.
鉴于我的场景,我应该使用Web API控制器还是MVC控制器?
我正在使用Web API在ASP.MVC MVC 4中创建一组新服务.到目前为止,这很棒.我已经创建了该服务并使其工作,现在我正在尝试使用JQuery来使用它.我可以使用Fiddler取回JSON字符串,它似乎没问题,但是因为该服务存在于一个单独的站点上,所以尝试使用"不允许"的JQuery错误调用它.所以,这显然是我需要使用JSONP的情况.
我知道Web API是新的,但我希望有人可以帮助我.
如何使用JSONP调用Web API方法?
在我的ApiController类中,我有以下方法来下载服务器创建的文件.
public HttpResponseMessage Get(int id)
{
try
{
string dir = HttpContext.Current.Server.MapPath("~"); //location of the template file
Stream file = new MemoryStream();
Stream result = _service.GetMyForm(id, dir, file);
if (result == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
result.Position = 0;
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(result);
return response;
}
catch (IOException)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
Run Code Online (Sandbox Code Playgroud)
除了默认下载文件名是其id之外,一切都工作正常,因此用户可能每次都需要在另存为对话框时键入他/她自己的文件名.有没有办法在上面的代码中设置默认文件名?
使用ASP.NET MVC控制器,您可以以不同的格式公开数据.AspNetWebAPI是专门为创建API而设计的,但我可以轻松地使用MVC控制器,我不清楚在什么情况下它会比传统的MVC控制器更好.我对WebApi的好处是显而易见的情况感兴趣,并且值得为我的应用程序添加另一个复杂性层.
问题:在MVC中使用asp.net WebApi有哪些优点和/或缺点?
我是ASP.NET MVC和Web API的新手,并试图获得基础知识.据我所知,我们在VS 2013项目模板,命名为MVC,Web API和Both of them together.
我已经完成了教程并了解到我们可以通过单独使用MVC以及使用Web API模板来创建API.
那么,基于架构和使用情况,它们之间有什么区别?
这两个实体是一对多的关系(由代码第一流畅的api构建).
public class Parent
{
public Parent()
{
this.Children = new List<Child>();
}
public int Id { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Data { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的WebApi控制器中,我有动作创建父实体(工作正常)并更新父实体(有一些问题).更新操作如下所示:
public void Update(UpdateParentModel model)
{
//what should be done here?
}
Run Code Online (Sandbox Code Playgroud)
目前我有两个想法:
获取一个名为existingby 的跟踪父实体model.Id,并将值model逐个赋值给实体.这听起来很愚蠢.在model.Children我不知道哪个孩子是新的,哪个孩子被修改(甚至删除).
通过创建一个新的父实体model …
asp.net-web-api ×10
asp.net-mvc ×5
c# ×4
asp.net ×2
json ×2
.net ×1
jquery ×1
jsonp ×1
postman ×1