我在以前版本的WCF Web API上使用了类似下面的方法:
// grab the posted stream
Stream stream = request.Content.ContentReadStream;
// write it to
using (FileStream fileStream = File.Create(fullFileName, (int)stream.Length)) {
byte[] bytesInStream = new byte[stream.Length];
stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
fileStream.Write(bytesInStream, 0, bytesInStream.Length);
}
Run Code Online (Sandbox Code Playgroud)
但在预览6中,HttpRequestMessage.Content.ContentReadStream财产已经消失.我相信它现在应该像这样:
// grab the posted stream
System.Threading.Tasks.Task<Stream> stream = request.Content.ReadAsStreamAsync();
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚在using语句中其余代码应该是什么样的.任何人都可以为我提供一种方法吗?
我在我的一个控制器中使用了以下代码;
if (Request.IsAjaxRequest()) {
return RedirectToAction("PreviewAndSendEmail");
}
Run Code Online (Sandbox Code Playgroud)
我调试它,它返回到行,但没有发生重定向.是否可以在Ajax.BeginForm中执行此操作?这是剃刀代码;
using(Ajax.BeginForm( new AjaxOptions { LoadingElementId = "loading" })) {
<b>Choose E-mail Template : </b>@Html.DropDownList("emailtemps")<br /><br />
<input type="submit" value="Preview & Send" />
<span id="loading" style="display: none;">
<img title="loading..." alt="load" src="@Url.Content("~/Content/App_Icons/gifs/loading.gif")"
</span>
}
Run Code Online (Sandbox Code Playgroud) 我的MVC3 Razor视图有以下视图模型:
public class UserProfileModel
{
public Person[] Persons { get; set; }
//some other fields
}
Run Code Online (Sandbox Code Playgroud)
我想在Razor视图中显示所有人:
foreach (var person in Model.Persons)
{
<div>
@* some custom formatting *@
@Html.Display(person)
</div>
}
Run Code Online (Sandbox Code Playgroud)
@Html.Display或@Html.DisplayFor似乎不适合我..
我可以使用Person模型创建一个单独的stongly类型视图并@Html.DisplayForModel在那里调用,但有没有办法在没有单独的视图的情况下去?
我的一个ASP.NET MVC 3应用程序中发生了一件奇怪的事情.
我通过jQuery Ajax api获取插入行,没有问题.但是,当我得到必要的部分视图时,它没有验证属性,我无法重新绑定这些行的验证.
这是我得到的ajax响应:
<input type="hidden" name="accommPropertyPeriods.index" autocomplete="off" value="ccaa15b3-76f1-4215-8bb5-a62d700bfc1e" />
<table style="width:100%;">
<tr>
<td>
<div class="editor-field">
<select class="chzn-select-deselect" data-placeholder="Choose an Alias..." id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__AccommPropertySeasonPeriodAliasID" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].AccommPropertySeasonPeriodAliasID" style="min-width:100px;"><option value="302">A</option>
<option value="303">B</option>
<option value="304">C</option>
<option value="305">D</option>
</select>
</div>
</td>
<td>
<div class="editor-field">
<input class="datefield" id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__PeriodStartsAt" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].PeriodStartsAt" type="text" value="" />
</div>
</td>
<td>
<div class="editor-field">
<input class="datefield" id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__PeriodEndsAt" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].PeriodEndsAt" type="text" value="" />
</div>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
这是我应该得到的:
<input type="hidden" name="accommPropertyPeriods.index" autocomplete="off" value="84ddd0f5-a3e2-4f10-8e67-f32528c6393d" />
<table style="width:100%;">
<tr>
<td>
<div class="editor-field">
<select class="chzn-select-deselect" data-placeholder="Choose an …Run Code Online (Sandbox Code Playgroud) asp.net-mvc asp.net-mvc-templates asp.net-mvc-validation asp.net-mvc-3
是否有意义使其他GitHub用户只能分叉我的私有存储库并向我发送拉取请求.我不希望他们直接将更改推送到我的私人仓库.
我在这里苦苦挣扎的是,用户无法看到我的私人仓库,这是一件好事.然后,我将用户作为协作者添加到我的仓库中,并且该用户现在具有对该私有仓库的推送访问权限,这是一件坏事.
如果有机会只允许用户访问我的私人仓库的特定分支,它对我也有用.
有什么想法吗?
关于这个主题,我问了另一个问题:
这是当前的情况:在我的ASP.NET MVC 3 App上,我有一个如下定义的路由约束:
public class CountryRouteConstraint : IRouteConstraint {
private readonly ICountryRepository<Country> _countryRepo;
public CountryRouteConstraint(ICountryRepository<Country> countryRepo) {
_countryRepo = countryRepo;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
//do the database look-up here
//return the result according the value you got from DB
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用如下:
routes.MapRoute(
"Countries",
"countries/{country}",
new {
controller = "Countries",
action = "Index"
},
new {
country = new CountryRouteConstraint(
DependencyResolver.Current.GetService<ICountryRepository<Country>>()
)
}
); …Run Code Online (Sandbox Code Playgroud) asp.net-mvc unit-testing dependency-injection moq asp.net-mvc-3
在我的viewmodel中,我有一个knockoutjs ObserableArray.在我初始化ViewModel之后,它成功地绑定了数据.然后,我需要做的是对集合进行排序.
$.each(vm.searchResults(), function (i, property) {
console.log(property.Name());
});
vm.searchResults().sort(function (a, b) {
return a.Name().toLowerCase() > b.Name().toLowerCase() ? 1 : -1;
});
$.each(vm.searchResults(), function (i, property) {
console.log(property.Name());
});
Run Code Online (Sandbox Code Playgroud)
如您所见,我将元素的名称输出到控制台,以查看排序前后的顺序.排序工作得很好.问题在于UI更新.不知何故,UI未更新.
然后,尝试使用以下代码从数组中删除记录,以查看UI是否将响应:
vm.searchResults().shift();
Run Code Online (Sandbox Code Playgroud)
用户界面保持不变,不再次更新.这会是什么问题?
编辑:
以下是一个示例案例:http://jsfiddle.net/tugberk/KLpwP/
同样的问题也在这里.
编辑:
我解决了这个问题所示的问题:http://jsfiddle.net/tugberk/KLpwP/16/但我仍然不确定为什么它起作用,因为我最初尝试过.
我不确定这是否是Covariance和Contravariance问题,但我不能让这个工作.这是代码:
public interface IDto { }
public class PaginatedDto<TDto> where TDto : IDto {
public int PageIndex { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
public int TotalPageCount { get; set; }
public bool HasNextPage { get; set; }
public bool HasPreviousPage { get; set; }
public IEnumerable<TDto> Dtos { get; set; }
}
public class PersonDto : IDto {
public int Id { get; set; }
public string Name …Run Code Online (Sandbox Code Playgroud) 目前,我面临着构建Web服务的要求,该服务将允许Java客户端通过httprequest检索并触发对我们数据的操作.我们是一个.Net商店,看来最新的解决方案是微软的MVC4 Web API.我已经习惯了你从API中提取数据的标准分层架构,但这将是我的第一个提供数据的Web服务.
从我的学习中我看到的建议如下:
我正在寻找具有MVC4 Web API经验的人,他可以通过这种方式阐明构建Web服务的良好实践.
提前致谢.
假设我正在构建一个ASP.NET Web API应用程序,它具有以下结构:

正如你从图中可以看到,的ASP.NET Web API的核心会跟域名服务层(如MembershipService类具有的方法,如GetUsers,CreateUser等)和我的服务类会跟一个或多个存储库来处理业务.
很明显,服务操作(例如MembershipService.CreateUser方法)会因为多种原因(未满足的条件,存储库抛出的异常等)而失败,这就是我怀疑的地方.
您认为服务类应该处理异常并返回某种结果对象,如下所示:
public class OperationResult {
public OperationResult(bool isSuccess) : this(isSuccess) {
IsSuccess = isSuccess;
}
public OperationResult(bool isSuccess, Exception exception) : this(isSuccess) {
Exception = exception;
}
public bool IsSuccess { get; private set; }
public Exception IsSuccess { get; private set; }
}
public class OperationResult<TEntity> : OperationResult {
public OperationResult(bool isSuccess)
: base(isSuccess) { }
public OperationResult(bool isSuccess, Exception exception)
: base(isSuccess, exception) …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×4
c# ×4
.net ×1
ajax ×1
asp.net ×1
asynchronous ×1
covariance ×1
git ×1
github ×1
javascript ×1
knockout.js ×1
moq ×1
razor ×1
unit-testing ×1
wcf-web-api ×1