如何将JSON数组发布到Web API?它适用于单个对象.
这是我试过的,但控制器似乎正在返回0而不是预期3.
这是我的JSON:
var sc = [{
"ID": "5",
"Patient_ID": "271655b8-c64d-4061-86fc-0d990935316a",
"Table_ID": "Allergy_Trns",
"Checksum": "-475090533",
"LastModified": "2015-01-22T20:08:52.013"
},
{
"ID": "5",
"Patient_ID": "271655b8-c64d-4061-86fc-0d990935316a",
"Table_ID": "Allergy_Trns",
"Checksum": "-475090533",
"LastModified": "2015-01-22T20:08:52.013"
},
{
"ID": "5",
"Patient_ID": "271655b8-c64d-4061-86fc-0d990935316a",
"Table_ID": "Allergy_Trns",
"Checksum": "-475090533",
"LastModified": "2015-01-22T20:08:52.013"
}];
Run Code Online (Sandbox Code Playgroud)
AJAX电话:
$.ajax({
url: urlString,
type: 'POST',
data: sc,
dataType: 'json',
crossDomain: true,
cache: false,
success: function (data) { console.log(data); }
});
Run Code Online (Sandbox Code Playgroud)
Web API控制器:
[HttpPost]
public string PostProducts([FromBody]List<SyncingControl> persons)
{
return persons.Count.ToString(); // 0, …Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.Net MVC5开发一个项目,该项目还包括一个Web API.API仅供内部使用.我正在使用OWIN库来提供身份验证.
我很难搞清楚如何通过API正确实现身份验证.我计划使用OAuth 2.0,但OAuth的问题是用户需要通过浏览器页面而不是本机登录屏幕登录.所以我想知道是否有可能以某种方式跳过浏览器.
我发现这个例子创建了自己的OAuth授权服务器.但它没有显示如何使登录本机.
我有一个类似的课程
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
public ICollection<Category> CategorySelected { get; set; }
public static List<Category> GetOptions()
{
var categories = new List<Category>();
categories.Add(new Category() { ID = 1, Name = "Bikes" });
categories.Add(new Category() { ID = 2, Name = "Cars" });
categories.Add(new Category() { ID = 3, Name = "Trucks" });
return categories;
}
}
Run Code Online (Sandbox Code Playgroud)
在控制器中,我填充MiltiselectItems并为其设置selectedValues
public ActionResult Index()
{
Category cat=new Category();
cat.CategorySelected.Add(new Category …Run Code Online (Sandbox Code Playgroud) 我有这个控制器,我想要做的是将图像作为[字节]发送到控制器,这是我的控制器:
[HttpPost]
public ActionResult AddEquipment(Product product, HttpPostedFileBase image)
{
if (image != null)
{
product.ImageMimeType = image.ContentType;
product.ImageData = new byte[image.ContentLength];
image.InputStream.Read(product.ImageData, 0, image.ContentLength);
}
_db.Products.Add(product);
_db.SaveChanges();
return View();
}
Run Code Online (Sandbox Code Playgroud)
在我看来:
@using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post)) {
<fieldset>
<legend>Product</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model …Run Code Online (Sandbox Code Playgroud) 我有一个简单的更新功能:
public void Update(Users user)
{
tblUserData userData = _context.tblUserDatas.Where(u => u.IDUSER == user.IDUSER).FirstOrDefault();
if (userData != null)
{
Mapper.CreateMap<Users, tblUserData>();
userData = Mapper.Map<Users, tblUserData>(user);
_context.SaveChanges()
}
}
Run Code Online (Sandbox Code Playgroud)
userData是一个EF实体,它的Entity Key属性被删除了,因为我相信它存在于目标对象中,但不存在于源对象中,因此它将使用其默认值进行映射(对于Entity Key,它是null )
所以,我的问题是,Automapper是否可以配置为仅尝试映射源对象和目标对象中存在的属性?我想要跳过实体键和导航属性之类的东西.
我刚开始用.NET编程,我在实现时遇到了一些问题dependency injection (using Ninject).
我正在创建某种餐饮应用程序,用户可以浏览城镇,城镇浏览餐馆和餐馆浏览食物.
我正在使用UnitOfWork和存储库模式,例如我通过id访问城镇,如下所示:
_unitOfWork.TownRepository.GetByID(id);
Run Code Online (Sandbox Code Playgroud)
现在我开始在应用程序中实现服务,我遇到了需要dependency injection.
我已经创建ITownService,IRestaurantService和IFoodService(因为我已经TownRepository,RestaurantRepository并且FoodRepository在我的UnitOfWork).
TownService的样本外观:
public class TownService : ITownService
{
// initialize UnitOfWork
private IUnitOfWork _unitOfWork;
public TownService()
: this(new UnitOfWork())
{
}
public TownService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public Town GetByID(object id)
{
return _unitOfWork.TownRepository.GetByID(id);
}
public IEnumerable<Town> GetAll()
{
return _unitOfWork.TownRepository.Get();
}
public bool Insert(Town town)
{
// validation logic
if …Run Code Online (Sandbox Code Playgroud) 在我的ASP.NET MVC4应用程序中,我有这样定义的模型:
public class Employee : BaseObject
{
[JsonIgnore]
public string FirstName { get; set; }
[JsonIgnore]
public string LastName { get; set; }
[JsonIgnore]
public string Manager { get; set; }
public string Login { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
当我使用ApiController返回此对象时,我得到没有具有JsonIgnore属性的字段的正确对象,但是当我尝试使用下面的代码在cshtml文件中添加相同的对象时,我得到所有字段.
<script type="text/javascript">
window.parameters = @Html.Raw(@Json.Encode(Model));
</script>
Run Code Online (Sandbox Code Playgroud)
看起来@Json.Encode忽略了这些属性.
怎么解决这个问题?
我有一个web项目的解决方案,我决定在解决方案中添加一个单元测试项目.在运行测试时,其中一个失败并出现此错误:
Result Message:
Test method DetailsRoleController threw exception:
System.InvalidOperationException: No connection string named 'EquipmentEntities' could be found in the application config file.
Run Code Online (Sandbox Code Playgroud)
这是我的测试脚本:
[TestClass]
public class RoleControllerTest
{
RoleController RC = new RoleController();
[TestMethod]
public void IndexRoleController()
{
}
[TestMethod]
public void DetailsRoleController()
{
var result = RC.Delete(1);
Assert.IsNotNull(result);
}
}
Run Code Online (Sandbox Code Playgroud)
和控制器方法:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Role role = db.Roles.Find(id);
if (role == null)
{
return HttpNotFound();
}
return View(role); …Run Code Online (Sandbox Code Playgroud) 随着Twitter在2013年6月11日关闭API 1.0水龙头,我们有几个网站现在无法显示时间表.我一直在寻找一个"如果你这样做,现在做这个"的例子.这是推特的宣布. https://dev.twitter.com/blog/api-v1-is-retired
以下是我们最初通过API 1.0显示Twitter时间线的内容.
<div id="twitter">
<ul id="twitter_update_list"></ul>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://api.twitter.com/1/statuses/user_timeline/companytwitterhandle.json?callback=twitterCallback2&count=1"></script>
<div style="float:left;"><a href="https://twitter.com/companytwitterhandle" target="_blank">@companytwitterhandle</a> | </div>
<div class="twitterimg"> </div>
</div>
Run Code Online (Sandbox Code Playgroud)
最初我尝试更改JavaScript参考URL中的版本,这样做无效.
<script type="text/javascript" src="http://api.twitter.com/1.1/statuses/user_timeline/companytwitterhandle.json?callback=twitterCallback2&count=1"></script>
Run Code Online (Sandbox Code Playgroud)
然后我查看了Twitter API文档(https://dev.twitter.com/docs/api/1.1/overview),该文档缺少明确的转换示例.我没有4或5个小时深入研究,或者进入这个蓬乱的常见问题解答(https://dev.twitter.com/docs/faq#17750).
然后我找到了关于用户时间线的API文档.所以我再次更改了URL,如下所示. https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
<script type="text/javascript" src="https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=companytwitterhandle&count=1"></script>
Run Code Online (Sandbox Code Playgroud)
那没用.
使用jQuery或C#ASP.NET MVC,如何将该接口从Twitter API 1.0转换到Twitter API 1.1?如果可能的话,我的第一个偏好是浏览器客户端实现.请包含一个代码示例.谢谢.
在Automapper中,在配置String属性的映射时是否可以将源与目标连接起来?我以为我可以这样做:
Mapper.CreateMap<Foo, Bar>()
.ForMember(d => d.Notes, opt => opt.MapFrom(s => d.Notes + s.Notes)));
...
Mapper.Map<Foo, Bar>(source, destination);
Run Code Online (Sandbox Code Playgroud)
但是,在MapFrom lambda中,d显然不在范围内.有关如何在我的映射中组合源值和目标值的任何建议?
c# ×9
asp.net ×8
asp.net-mvc ×6
automapper ×2
jquery ×2
json ×2
razor ×2
ninject ×1
oauth-2.0 ×1
owin ×1
twitter ×1
unit-testing ×1