我的应用程序在迁移到beta 6/7之后停止工作,在调查之后,我发现我的json反序列化器不再使用(Jil),它被要求写入而不是用于阅读.
现在已经3天了,我正在搜索论坛并阅读aspnet代码,但我还没有找到问题.
我注意到JSON.net在beta 6中无处不在,在beta 7中稍微少一点.
这是我的代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore(options =>
{
var jilFormatter = new JilMediaTypeFormatter();
options.OutputFormatters.Clear();
options.OutputFormatters.Add(jilFormatter);
options.InputFormatters.Clear();
options.InputFormatters.Add(jilFormatter);
options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json"));
options.ModelBinders.Add(new DocumentModelBinder());
options.ModelBinders.Add(new DataTablesBinder());
});
services.AddDataProtection();
services.AddWebEncoders();
}
Run Code Online (Sandbox Code Playgroud)
即使我只是在没有添加对象的情况下执行InputFormatters.Clear(),它也会对请求进行反序列化,我不知道它是如何做到的.
我的JIL InputFormatter/OutputFormatter(ouputformatter工作,我可以在CanWrite中断,但CanRead没有任何反应)
internal class JilMediaTypeFormatter : IOutputFormatter, IInputFormatter
{
private static readonly string [] _readableTypes = new[] { "application/json", "text/json", "text/javascript" };
private static readonly Task<bool> _done = Task.FromResult(true);
private readonly Options _options = Options.RFC1123ExcludeNullsIncludeInherited;
public bool CanWriteResult(OutputFormatterContext context, Microsoft.Net.Http.Headers.MediaTypeHeaderValue contentType)
{
return contentType ==null …Run Code Online (Sandbox Code Playgroud) 对于 MVC4,通过 将ViewModel用于填充视图的视图发送回控制器的最佳实践方法是POST什么?
我正在使用MVC 5中的Entity Framework 6.
我有以下方法:
[HttpPost]
public ActionResult UpdateDetails(ApplicationUser applicationUser)
{
var context = new ApplicationDbContext();
var user = context.Users.Select(x => x.UserName == applicationUser.UserName).FirstOrDefault();
//etc etc
}
Run Code Online (Sandbox Code Playgroud)
用户是一个IDbSet<ApplicationUser>.
为什么我从Select方法中得到一个bool?
我的期望是找回一个ApplicationUser物体.为什么不是这种情况?
谢谢
我想从数据库填充下拉列表,
我正在关注此链接
我在模型中编写了以下代码
public class FillDeptName
{
public IList<SelectListItem> Drp_Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
找不到类型或命名空间名称'SelectListItem'(您是否缺少using指令或程序集引用?)
当我需要一些并行处理时,通常会这样:
static void Main(string[] args)
{
var tasks = new List<Task>();
var toProcess = new List<string>{"dog", "cat", "whale", "etc"};
toProcess.ForEach(s => tasks.Add(CanRunAsync(s)));
Task.WaitAll(tasks.ToArray());
}
private static async Task CanRunAsync(string item)
{
// simulate some work
await Task.Delay(10000);
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我无法并行处理这些项目,而不得不使用Task.Run它来强制它在不同的线程上运行。
我想念什么?
我有一个果园项目,我在MVC中创建了一个模块.我想使用@ Html.ActionLink将特定用户的id传递给控制器,但它不会调用控制器.这是我的代码:
在视图中:
@Html.ActionLink("100111", "AddToCart", "ShoppingCart", new { id = 101 }, null)
//also tried,
@Html.ActionLink("102829", "AddToCart", "ShoppingCart", new { id = 1, area = "OnlineShopping" },null)
Run Code Online (Sandbox Code Playgroud)
在控制器中:
[HttpPost]
public ActionResult AddToCart(int id)
{
_shoppingCart.Add(id, 1);
return RedirectToAction("Index");
}
[Themed]
public ActionResult Index()
{
// Create a new shape using the "New" property of IOrchardServices.
var shape = _services.New.ShoppingCart();
// Return a ShapeResult
return new ShapeResult(this, shape);
}
Run Code Online (Sandbox Code Playgroud) 没有代码,因为我不知道这是否可能.
是否有可能在Db(此时创建)中使用角色Seed()作为Enum或类似的东西,以便不使用记住所有角色等,而是可以使用intellisense?
如果有可能你能告诉我怎么样?
当需要在动作API中创建文件夹时,是否会涉及任何并发问题?如果是这样,正确的解决方案是使用锁,如下面的代码所示
public async Task<IActionResult> UploadFile(User user){
...
var file = HttpContext.Request.Form.File["SomeFile"];
...
var path = Path.Combile(hostingEnvironment.WebRoot, configurationRoot["BaseDirectory"], user.Id);
lock(path){
if(!Directory.Exists(path)){
Directory.CreateDirectory (path);
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
将path变量用作锁定对象也是合乎逻辑的吗?
c# ×8
asp.net-mvc ×3
asp.net-core ×2
.net ×1
actionlink ×1
concurrency ×1
enums ×1
intellisense ×1
linq ×1
orchardcms ×1
post ×1
razor ×1