我正在关注Chris Pietschmann 在ASP.NET MVC中使用主题的解决方案.
我注意到的一件事是在后续请求中没有从ViewLocationCache中检索视图名称.我正在使用ASP.NET MVC 2.0 RC
执行以下代码时:
this.ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, virtualPath);
Run Code Online (Sandbox Code Playgroud)
我将鼠标悬停在this.ViewLocationCache上它只返回{System.Web.Mvc.NullViewLocationCache} - 建议什么都没有添加?
据布拉德·威尔逊的RenderAction 是比慢的RenderPartial.
但是,有没有人有任何显示性能差异的统计数据?
我正在开发一个页面由"Widgets"组成的应用程序.
我有两个选择:
视图级别的组合
为每个小部件调用RenderAction.这是迄今为止最简单的方法,但这意味着我们正在为每个小部件执行完整的MVC循环.
控制器级别的组成
为包含每个小部件所需数据的页面构建一个ViewModel.为每个小部件调用RenderPartial.这实现起来要复杂得多,但这意味着我们只会制作一个MVC周期.
我在页面上用3个不同的小部件测试了上述方法,渲染时间的差异是十分之一秒(几乎不值得担心).
但是,有没有人比这更具体的测试结果,或者尝试两种方法?
我们有一个应用程序分为两部分:
我正在寻找创建REST API来提供此功能.很容易看出如何表示CRUD操作,但我不确定单个资源上的特定操作(命令).例如,为了"发布",Project我们发送一个"PublishCommand".我们不会将Project其Published属性设置为的完全返回到服务器true.
在类似的说明中,我对如何在不被归类为RPC类型服务的情况下代表更高级的资源查询操作感到困惑.
下面我列出了我的Project资源的URI模板.我是否正在创建真正的RESTful API?
ADMIN API
---------
// Project Resources
GET /projects -- get all projects
POST /projects -- create a new project
// Project Resource
GET /projects/10 -- get project with id 10
PUT /projects/10 -- update project with id 10
DELETE /projects/10 -- delete project with id 10
// Project Resource Operations
POST: /projects/10/publish -- publish project with id 10 …Run Code Online (Sandbox Code Playgroud) 目前,JSON.NET忽略了实现IEnumerable的类的所有其他属性并序列化了数组.
如何告诉JSON.NET序列化自定义属性?我正在尝试序列化以下PagedList<T>实现:
public interface IPagedList : IEnumerable
{
int PageIndex { get; }
int PageSize { get; }
int TotalCount { get; }
int TotalPages { get; }
bool HasPreviousPage { get; }
bool HasNextPage { get; }
}
public interface IPagedList<T> : IPagedList, IList<T>
{
}
/// <summary>
/// A tried and tested PagedList implementation
/// </summary>
public class PagedList<T> : List<T>, IPagedList<T>
{
public PagedList(IEnumerable<T> source, int pageIndex, int pageSize) :
this(source.GetPage(pageIndex, pageSize), pageIndex, pageSize, source.Count()) …Run Code Online (Sandbox Code Playgroud) 是否建议在原型对象上创建计算属性?
这是我在下面尝试的但firstName绑定是将函数作为字符串返回而不是执行它(http://jsfiddle.net/W37Yh).
var HomeViewModel = function(config, $, undefined) {
if (!this instanceof HomeViewModel) {
return new HomeViewModel(config, $, undefined);
}
this.firstName = ko.observable(config.firstName);
this.lastName = ko.observable(config.lastName);
};
HomeViewModel.prototype.fullName = function() {
return ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this);
};
var model = new HomeViewModel({
firstName: "John",
lastName: "Smith"
}, jQuery);
ko.applyBindings(model);?
Run Code Online (Sandbox Code Playgroud) 根据标题,是否有必要在调用之前等待DOM加载,ko.applyBindings或者Knockout会自动处理吗?
即 - 我可以安全地做到:
<script>
(function() {
var model = new my.Model();
ko.applyBindings(model);
})();
</script>
Run Code Online (Sandbox Code Playgroud) 我正在使用jQuery执行AJAX POST,如下所示:
self.post = function (path, data) {
return $.ajax({
url: this.createUri(path),
type: "POST",
contentType: "application/json",
dataType: "json",
data: ko.toJSON(data)
});
};
Run Code Online (Sandbox Code Playgroud)
这里我只返回AJAX Deferred对象.响应由另一个对象处理:
api.post(menuItemsUri, self.newItem)
.done(function (data, textStatus, request) {
console.log(request.getResponseHeader("Location")); // undefined
})
.always(function () {
// reset the current item
self.newItem.update({});
});
Run Code Online (Sandbox Code Playgroud)
服务器返回201 Created响应并设置Location标头.我可以在Chrome网络标签中看到这一点:
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Length:0
Date:Thu, 07 Feb 2013 10:25:04 GMT
Expires:-1
Location:http://localhost:49978/sites/1/menus/65/items/19
Pragma:no-cache
Run Code Online (Sandbox Code Playgroud)
但是,XmlHttpRequest在jQuery AJAX回调中传递的对象中缺少Location标头.
我从Illustrator导出了一个SVG,想要设置SVG的高度并按比例增加宽度比例.
这是SVG的示例:
<svg xmlns="http://www.w3.org/2000/svg" width="20px" height="10px" viewBox="0 0 20 10">
<path fill="#124C92" d="M20,7c0,1.65-1.35,3-3,3H3c-1.65,0-3-1.35-3-3V3c0-1.65,1.35-3,3-3h14c1.65,0,3,1.35,3,3V7z" />
</svg>
Run Code Online (Sandbox Code Playgroud)
如果我只设置高度,则SVG宽度设置为100%.
到目前为止,我一直无法找到一个不需要我设置高度和宽度的解决方案.
我正在测试一个MVC控制器,它依赖于从基类的只读属性返回的值.
这个属性的getter在被调用时抛出一个异常,因为它依赖于HttpContext(和其他令人讨厌的东西),我宁愿避免嘲笑.
这是我到目前为止所尝试的:
controller = Substitute.ForPartsOf<MyController>(
Substitute.For<SomeDependency>(),
);
controller.UserInfo.Returns(new UserInfo());
Run Code Online (Sandbox Code Playgroud)
然而,只要访问UserInfo,就会抛出异常.
基类的属性是:
public UserInfo UserInfo
{
get
{
// HttpContext dependent stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试将基类属性设置为虚拟,但后来我得到了Castle代理异常.
我们的ASP.NET MVC应用程序允许经过身份验证的用户管理链接到其帐户的一个或多个"站点".
我们的网址是高度猜测的,因为我们在网址而不是Id中使用网站友好名称,例如:
/sites/mysite/
/sites/mysite/settings
/sites/mysite/blog/posts
/sites/mysite/pages/create
Run Code Online (Sandbox Code Playgroud)
如您所见,我们需要在多个路线中访问站点名称.
我们需要为所有这些操作执行相同的行为:
我们始终可以通过ISiteContext对象使用当前帐户.以下是我如何使用普通路由参数实现上述所有操作并直接在我的操作中执行查询:
private readonly ISiteContext siteContext;
private readonly IRepository<Site> siteRepository;
public SitesController(ISiteContext siteContext, IRepository<Site> siteRepository)
{
this.siteContext = siteContext;
this.siteRepository = siteRepository;
}
[HttpGet]
public ActionResult Details(string id)
{
var site =
siteRepository.Get(
s => s.Account == siteContext.Account && s.SystemName == id
);
if (site == null)
return HttpNotFound();
return Content("Viewing details for site " + site.Name);
}
Run Code Online (Sandbox Code Playgroud)
这不是太糟糕,但我需要在20个左右的动作方法上做这个,所以想尽可能保持干燥.
我没有做过自定义模型粘合剂,所以我想知道这是否是一个更适合他们的工作.一个关键要求是我可以将我的依赖项注入模型绑定器(对于ISiteContext和IRepository - 如果需要,我可以回退到DependencyResolver).
非常感谢,
本
更新 …
asp.net-mvc ×3
c# ×2
knockout.js ×2
ajax ×1
asp.net ×1
jquery ×1
json.net ×1
mocking ×1
modelbinders ×1
nsubstitute ×1
rest ×1
svg ×1
unit-testing ×1
viewengine ×1