Tag: asp.net core 1.0

如何在ASP.NET MVC 6中继续使用ModelState和RedirectToAction?

我有一个删除对象的方法。 删除不属于视图,并且是“EditReport”中的“删除”按钮。 成功删除“报告”上的重定向后。 [HttpPost] [Route(“{reportId:int}”)] [ValidateAntiForgeryToken] public IActionResult DeleteReport(int reportId) { var success = _reportService.DeleteReportControl(reportId); if (success == false) { ModelState.AddModelError(“Error”, “Messages”); return RedirectToAction(“EditReport”); } ModelState.AddModelError(“OK”, “Messages”); return RedirectToAction(“Report”); } 在ASP.NET MVC 5中,我使用以下属性在方法之间保存ModelState。 我从这里开始: https : //stackoverflow.com/a/12024227/3878213 public class SetTempDataModelStateAttribute : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); filterContext.Controller.TempData[“ModelState”] = filterContext.Controller.ViewData.ModelState; } } […]

没有为“Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory”类型提供服务

我遇到了这个问题: 没有注册“Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory”类型的服务。 在asp.net核心1.0中,似乎当动作尝试渲染视图时我有exception。 我搜索了很多,但我没有找到解决方案,如果有人可以帮我弄清楚发生了什么,我怎么能解决它,我会很感激。 我的代码如下: 我的project.json文件 { “dependencies”: { “Microsoft.NETCore.App”: { “version”: “1.0.0”, “type”: “platform” }, “Microsoft.AspNetCore.Diagnostics”: “1.0.0”, “Microsoft.AspNetCore.Server.IISIntegration”: “1.0.0”, “Microsoft.AspNetCore.Server.Kestrel”: “1.0.0”, “Microsoft.Extensions.Logging.Console”: “1.0.0”, “Microsoft.AspNetCore.Mvc”: “1.0.0”, “Microsoft.AspNetCore.StaticFiles”: “1.0.0-rc2-final”, “EntityFramework.MicrosoftSqlServer”: “7.0.0-rc1-final”, “EntityFramework.Commands”: “7.0.0-rc1-final” }, “tools”: { “Microsoft.AspNetCore.Server.IISIntegration.Tools”: “1.0.0-preview2-final” }, “frameworks”: { “netcoreapp1.0”: { “imports”: [ “dnxcore50”, “portable-net45+win8” ] } }, “buildOptions”: { “emitEntryPoint”: true, “preserveCompilationContext”: true […]

ASPNET核心服务器发送事件/响应刷新

虽然没有官方文档,但是有谁知道如何使用ASP.NET Core实现SSE? 我怀疑一个实现可能会使用自定义中间件,但也许可以在控制器操作中执行此操作?

ASP.NET Core RC2种子数据库

我的问题是我试图用数据种子entity framework核心数据库,在我看来下面的代码显示工作。 我已经意识到这不应该在ApplicationDbContext构造函数中调用,应该从startup调用,但我不知道如何做到这一点。 编辑:根据Ketrex提供的解决方案,我的解决方案如下: Startup.cs: public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { … app.ApplicationServices.GetRequiredService().Seed(); } 种子延伸: public static class DbContextExtensions { public static void Seed(this ApplicationDbContext context) { // Perform database delete and create context.Database.EnsureDeleted(); context.Database.EnsureCreated(); // Perform seed operations AddCountries(context); AddAreas(context); AddGrades(context); AddCrags(context); AddClimbs(context); // Save changes and release resources context.SaveChanges(); context.Dispose(); […]

在.NET Core中使用DataTable

我在SQL Server中有一个存储过程,它接受用户定义的表类型。 我正在按照这篇文章的答案从C#list批量插入SQL Server到具有外键密码的多个表中,如何将DataTable发送到SQL中的存储过程。 但是当我创建DataTable table = new DataTable(); 我收到一个错误, DataTable does not contain a constructor that takes 0 arguments 。 我发现这个https://github.com/VahidN/EPPlus.Core/issues/4基本上说.NET Core不再支持DataTable 。 那么现在怎么办? 如何创建DataTable(或者它的替代品是什么)? 如何将用户定义的表类型发送到.NET Core上的SQL Server?

在ASP.NET核心中注入IUrlHelper

在RC1中 , IUrlHelper可以在服务中注入services.AddMvc()在启动类中使用services.AddMvc() ) 这在RC2中不再起作用。 有没有人知道如何在RC2中做到这一点,因为刚刚新建一个UrlHelper需要一个ActionContext对象。 不知道如何在控制器之外得到它。

如何在ASP.NET Core 1.0的DI中的Startup类中添加IHttpContextAccessor?

在ASP.NET Core RC 1中,我使用以下代码来检索上下文的值(页面的完整地址)。 然后我在配置中记录了这个值。 public class Startup { public IConfigurationRoot Configuration { get; set; } private IHostingEnvironment CurrentEnvironment { get; set; } private IHttpContextAccessor HttpContextAccessor { get; set; } public Startup(IHostingEnvironment env, IHttpContextAccessor httpContextAccessor) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile(“appsettings.json”, optional: true, reloadOnChange: true) .AddJsonFile($”appsettings.{env.EnvironmentName}.json”, optional: true); if (env.IsDevelopment()) { builder.AddUserSecrets(); } […]

覆盖ASP.NET Core MVC 1.0中的全局授权filter

我试图在ASP.NET Core 1.0(MVC 6)Web应用程序中设置授权。 更严格的方法 – 默认情况下,我希望将所有控制器和操作方法限制为具有Admin角色的用户。 所以,我正在添加一个全局授权属性,如: AuthorizationPolicy requireAdminRole = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .RequireRole(“Admin”) .Build(); services.AddMvc(options => { options.Filters.Add(new AuthorizeFilter(requireAdminRole));}); 然后我想允许具有特定角色的用户访问具体的控制器。 例如: [Authorize(Roles=”Admin,UserManager”)] public class UserControler : Controller{} 这当然是行不通的,因为“全局filter”不允许UserManager访问控制器,因为它们不是“管理员”。 在MVC5中,我能够通过创建自定义授权属性并将我的逻辑放在那里来实现这一点。 然后使用此自定义属性作为全局。 例如: public class IsAdminOrAuthorizeAttribute : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { ActionDescriptor action = filterContext.ActionDescriptor; if (action.IsDefined(typeof(AuthorizeAttribute), true) || action.ControllerDescriptor.IsDefined(typeof(AuthorizeAttribute), true)) […]

error handling(将ex.Message发送到客户端)

我有一个ASP.NET Core 1.0 Web API应用程序,并尝试弄清楚如果我的控制器调用的函数错误输出exception消息到客户端。 我尝试了很多东西,但没有实现IActionResult 。 我不明白为什么这不是人们需要的常见事情。 如果真的没有解决方案可以有人告诉我为什么? 我确实使用HttpResponseException(HttpResponseMessage)看到了一些文档,但为了使用它,我必须安装compat shim。 在Core 1.0中有没有新的方法来做这些事情? 这是我一直在尝试垫片,但它不起作用: // GET: api/customers/{id} [HttpGet(“{id}”, Name = “GetCustomer”)] public IActionResult GetById(int id) { Customer c = _customersService.GetCustomerById(id); if (c == null) { var response = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(“Customer doesn’t exist”, System.Text.Encoding.UTF8, “text/plain”), StatusCode = HttpStatusCode.NotFound }; throw new […]