Tag: asp.net core mvc

exception处理中间件和页面

我是中间件概念的新手,我目前正在努力处理我的MVC Core项目中的exception。 我想要发生的是捕获,记录exception,然后将用户发送到带有消息的友好错误页面。 起初,我试图在中间件中管理所有这些,但意识到我可能没有正确地做到这一点。 因此,如果我想要这个流程发生,我应该使用我的exception日志记录中间件和app.UseExceptionHandler(“/ Error”),以便中间件重新抛出exception到页面? 如果是这样,我如何在错误页面中获取exception详细信息? 我想首先拦截的exception处理机制应该是Configure中的最后一个吗?这是正确的吗? 我发现的所有示例都严格处理HTTP状态代码错误,如404; 我正在寻找处理实际的exception(及其子类)。 这样,在我的View页面中,我可以在适用的情况下抛出自己的exception(例如,如果为必填字段提供了一个null。) Startup.cs片段: public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, LoanDbContext context) { loggerFactory.AddConsole(Configuration.GetSection(“Logging”)); loggerFactory.AddDebug(); app.UseStatusCodePages(); if (env.IsDevelopment() || env.IsEnvironment(“qa”)) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler(“/Error”); } app.UseMiddleware(loggerFactory); // … rest of Configure is irrelevant to this issue MyExceptionHandler.cs using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using […]

如何在动作filter中获取当前模型

我有一个通用的动作filter,我想在OnActionExecuting方法中获取当前模型。 我目前的实现如下: public class CommandFilter : IActionFilter where T : class, new() { public void OnActionExecuting(ActionExecutingContext actionContext) { var model= (T)actionContext.ActionArguments[“model”]; } } 如果我的所有型号名称相同,它的效果很好。 但我想使用不同的模型名称。 如何解决这个问题呢? 编辑 public class HomeController : Controller { [ServiceFilter(typeof(CommandActionFilter))] public IActionResult Create([FromBody]CreateInput model) { return new OkResult(); } }

在MVC 6中读取文件

我想在我的服务器的主文件夹中访问我的create.sql文件。 它包含用于设置数据库的查询。 我有一个问题,根本无法访问此文件。 1)我无法通过Configuration 。 我只能使用AddJsonFile , AddXmlFile和AddIniFile 。 而且我认为将一个大的sql文件放入其中的任何一个都不是最好的主意。 2) github上的Mvc源似乎缺少MapPath 。 因此不可能使用Server.MapPath(“~/create.sql”) 。 那么如何实现呢?

如何使用mvc 6,asp.net 5注册全局filter

我正在尝试在简单的mvc应用程序中注册filter。 我甚至没有在基本filter之外编写任何东西来测试。 我正在使用VS 2015 RC,我通过转到新项目 – > Asp.net Web应用程序 – > Web API创建了初始应用程序。 我遇到的问题是我无法找到一种全局注册filter的方法。 从早期版本的MVC我看到GlobalFilters.Filters ,但是当我尝试在新框架中使用它时,它告诉我无法找到GlobalFilters。 在以前的版本中,它存在于System.Web.MVC中,但我不再在我的引用中看到它,我似乎无法在任何地方找到它。 这看起来应该很简单,但到目前为止我还没有找到办法。 这是我的project.json { “webroot”: “wwwroot”, “version”: “1.0.0-*”, “dependencies”: { “Microsoft.AspNet.Mvc”: “6.0.0-beta4”, “Microsoft.AspNet.Server.IIS”: “1.0.0-beta4”, “Microsoft.AspNet.Server.WebListener”: “1.0.0-beta4”, “Microsoft.AspNet.StaticFiles”: “1.0.0-beta4” }, “commands”: { “web”: “Microsoft.AspNet.Hosting –server Microsoft.AspNet.Server.WebListener –server.urls http://localhost:5000” }, “frameworks”: { “dnx451”: { “frameworkAssemblies”: { } }, “dnxcore50”: { } […]

Azure Web应用程序服务,使用混合连接管理器使用HttpClient调用onpremise WEB API

我们在本地网络机器上部署了Web服务(asp.net core mvc)。 我们正尝试使用部署在Azure上的App Service来调用这些WEB API。 但是当我们尝试使用“HTTP”协议连接它时,它会给出超时错误或任务被取消错误。 在“HTTPS”的情况下,它给出“ 发生安全错误错误 ”。 我们在Azure App Service上创建了Hybrid连接,以连接到onpremise web api服务,该服务在线显示80和443端口。 我们也在本地网络机器上设置了Hybrid Connection Manager。 以下是用于调用代码的代码段,该代码段部署在Azure App Service上(例如https://xyz.azurewebsite.com ) try { var httpClient = new HttpClient(); httpClient.Timeout = TimeSpan.FromMinutes(60); httpClient.BaseAddress = new Uri(“https://onpremise”); httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”)); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; //Simple […]

dependency injection,注入参数

我正在使用DI的vNext实现。 如何将参数传递给构造函数? 例如,我有课: public class RedisCacheProvider : ICacheProvider { private readonly string _connectionString; public RedisCacheProvider(string connectionString) { _connectionString = connectionString; } //interface methods implementation… } 和服务注册: services.AddSingleton(); 如何将参数传递给RedisCacheProvider类的构造函数? 例如Autofac: builder.RegisterType() .As() .WithParameter(“connectionString”, “myPrettyLocalhost:6379”);

无法让ASP.NET MVC 6 Controller返回JSON

我有一个MVC 6项目,我正在使用Fiddler来测试Web API。 如果我采取以下控制器操作,使用EntityFramework 7返回List。 然后html将呈现正常。 [HttpGet(“/”)] public IActionResult Index() { var model = orderRepository.GetAll(); return View(model); } 但是当我尝试返回Json响应时,我得到502错误。 [HttpGet(“/”)] public JsonResult Index() { var model = orderRepository.GetAll(); return Json(model); } 关于为什么对象没有正确序列化为json的任何想法?

我应该如何在MVC Core中管理DbContext Lifetime?

来自文档 应使用Scoped生存期将entity framework上下文添加到服务容器中。 如果您使用如上所示的辅助方法,则会自动完成此操作。 将使用entity framework的存储库应使用相同的生命周期。 我一直认为,我应该为我必须处理的每一个工作单元创建一个新的Context 。 这让我想一想,如果我有一个ServiceA和ServiceB ,它们在DbContext上应用不同的操作,他们应该得到一个不同的DbContext实例。 文档内容如下: Transient物体总是不同的; 为每个控制器和每个服务提供一个新实例。 Scoped对象在请求中是相同的,但在不同的请求中是不同的 回到ServiceA和ServiceB ,对我来说, Transient更适合。 我研究过,每个HttpRequest只能保存一次Context,但我真的不明白这是如何工作的。 特别是如果我们看一个服务: using (var transaction = dbContext.Database.BeginTransaction()) { //Create some entity var someEntity = new SomeEntity(); dbContext.SomeEntity.Add(someEntity); //Save in order to get the the id of the entity dbContext.SaveChanges(); //Create related entity var relatedEntity = new RelatedEntity { […]

如何在asp.net中的动作filter中添加参数?

我有以下filter属性,我可以将字符串数组传递给属性,如[MyAttribute(“string1”, “string2”)] 。 public class MyAttribute : TypeFilterAttribute { private readonly string[] _ids; public MyAttribute(params string[] ids) : base(typeof(MyAttributeImpl)) { _ids = ids; } private class MyAttributeImpl : IActionFilter { private readonly ILogger _logger; public MyAttributeImpl(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger(); } public void OnActionExecuting(ActionExecutingContext context) { // HOW DO I ACCESS THE IDs VARIABLE […]

ASP.NET MVC Core / 6:多个提交按钮

我需要多个提交按钮来在控制器中执行不同的操作。 我在这里看到了一个优雅的解决方案: 如何在ASP.NET MVC框架中处理多个提交按钮? 使用此解决方案,可以使用自定义属性修饰操作方法。 处理路径时,此自定义属性的方法检查属性的属性是否与单击的提交按钮的名称匹配。 但是在MVC Core(RC2每晚构建)中我还没有找到ActionNameSelectorAttribute (我还搜索了Github存储库)。 我找到了一个类似的解决方案,它使用ActionMethodSelectorAttribute ( http://www.dotnetcurry.com/aspnet-mvc/724/handle-multiple-submit-buttons-aspnet-mvc-action-methods )。 ActionMethodSelectorAttribute可用,但方法IsValidForRequest具有不同的签名。 有一个类型为RouteContext的参数。 但我找不到那里的post数据。 所以我没有什么可以与我的自定义属性属性进行比较。 MVC Core中是否有与之前的MVC版本类似的优雅解决方案?