我正在使用Knockout和MVC以及我看到的标准方法来获得淘汰视图模型是这样的:
var model = '@Html.Raw(Json.Encode(Model))';
var viewModel = ko.mapping.fromJSON(model);
Run Code Online (Sandbox Code Playgroud)
但是如果我的模型中包含特殊字符的字符串属性,例如'\ r \n',我会得到一个JSON解析错误'意外令牌'.
所以我相信我需要逃避这些角色,因此它们就像'\\ r \\n'.这该怎么做?
我知道我可以针对这个特殊情况做这件事:
var model = '@Html.Raw(Json.Encode(Model).Replace(@"\", @"\\"))';
Run Code Online (Sandbox Code Playgroud)
但可能还有其他...标签,单引号.
下面是浏览器中实际渲染模型的示例:
var model = '{"Id":4465,"TextContents":["EYE FILLET STEAK\r\nLINE 2 IS HERE"]}';
Run Code Online (Sandbox Code Playgroud) 我试图映射具有子对象的集合,我发现忽略同一类型的对象的()应用到性能子对象上似乎嗯...忽视!
这是一个演示问题的单元测试.
class A
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<B> Children { get; set; }
}
class B
{
public int Id { get; set; }
public string Name { get; set; }
}
[TestClass]
public class UnitTest1
{
[TestInitialize()]
public void Initialize()
{
Mapper.CreateMap<A, A>()
.ForMember(dest => dest.Id, opt => opt.Ignore());
Mapper.CreateMap<B, B>()
.ForMember(dest => dest.Id, opt => opt.Ignore());
}
[TestMethod]
public void TestMethod1()
{
A src = …Run Code Online (Sandbox Code Playgroud) 我试图使用Trace.CorrelationManager.ActivityId来关联日志条目.但是,我发现此代码完成时:
var result = await Task.Run(() => LongRunningMethod());
Run Code Online (Sandbox Code Playgroud)
ActivityId已经从输入时改变了.在LongRunningMethod()中它是正确的(我在方法中有各种跟踪事件),它似乎只在await完成时改变.
我的问题是为什么ActivityId改变了?
这行代码在使用async声明的函数中,而async又由MVC项目中的异步控制器操作调用:
async public Task<ActionResult> Index()
{
...
var tasks = {list of Download<T> delegates}
var result = await Task.WhenAll(tasks)
}
async public Task<OperationResult> Download<T>(IEnumerable<T> data, Device device)
{
...
var result = await Task.Run(() => LongRunningMethod());
return result
}
Run Code Online (Sandbox Code Playgroud)
也许我错误地使用async/await或Task方法?我基本上希望所有'LongRunningMethod'同时异步启动,然后等到所有完成.
是否可以在web.config文件中指定一些选项?在创建新项目时,默认情况下会获得此启动类,而旧的表单身份验证部分是web.config.
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
// Use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
Run Code Online (Sandbox Code Playgroud)
我希望能够在此处列出的CookieAuthenticationOptions上指定一些选项:
在web.config中(例如,到期超时).
这些对象具有ICollection <>类型的集合
如果我将对象图从客户端传递到服务器,则会抛出以下异常:
System.NotSupportedException was unhandled by user code
Message=Collection was of a fixed size.
Source=mscorlib
Run Code Online (Sandbox Code Playgroud)
在T4模板生成的修正代码中会发生这种情况.似乎集合在服务器上被反序列化为数组,因此无法修改.有没有办法指定序列化程序应该使用的类型?
asp.net-mvc ×2
c# ×2
async-await ×1
automapper ×1
json ×1
knockout.js ×1
owin ×1
poco ×1
tracesource ×1
wcf ×1