Tag: asp.net core

将global.asax迁移到ASP.NET 5

几天前.NET Core RC1已经发布了,我在阅读了很多关于它之后第一次试了一下,我喜欢它,但它有点不同。 我正在尝试将一个小博客(内置MVC5)迁移到MVC 6和.NET Core。 这并不难,但我真的很难重新创建我在MVC 5中完全相同的global.asax设置,ASP.NET 5不再具有global.asax所以我无法弄清楚大多数设置的替代品是什么是? protected void Application_Start() { ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new RazorViewEngine()); MvcHandler.DisableMvcResponseHeader = true; AntiForgeryConfig.SuppressXFrameOptionsHeader = true; BundleConfig.RegisterBundles(BundleTable.Bundles); RouteConfig.RegisterRoutes(RouteTable.Routes); } protected void Application_BeginRequest() { Response.AddHeader(“X-Frame-Options”, “DENY”); } protected void Application_EndRequest() { if (Response.StatusCode != 301 && Response.StatusCode != 302) return; var targetUrl = Response.RedirectLocation.Replace(“ReturnUrl”, “url”); Response.RedirectLocation = targetUrl; } protected […]

部署在iis上的asp.net核心应用程序遇到500内部服务器错误

:(糟糕.500内部服务器错误启动应用程序时出错。 当我向我的asp.net核心应用程序添加数据库function并将其部署到iis时,会出现此消息。 当我在Visual Studio中开发它时,一切都很顺利。 但部署后,出现此错误消息。 我尝试使用dotnet myapp.dll在deploy文件夹中运行我的应用程序,并发现它运行良好。 问题很可能与iis有关。 我试图将..到web.config,但它似乎没用。 实际上有什么问题,或者有没有其他方法可以查看详细的错误信息以帮助找出发生了什么?

使用Ocelot重新路由错误asp.net Core(7.0.4)

“ReRoutes”: [ { “DownstreamPathTemplate”: “/api/Agent/GetPagedAgents?page={page}”, “DownstreamScheme”: “http”, “DownstreamHostAndPorts”: [ { “Host”: “agent.api”, “Port”: 80 } ], “UpstreamPathTemplate”: “/api/account/user/list/GetPagedAgents?page={page}”, “UpstreamHttpMethod”: [] }] 在这里,我试图从查询字符串重新路由我的UpstreamPathTemplate到DownstreamPathTemplate, “http://accountmanagement/api/account/user/list/GetPagedAgents?page=1” 这是我的查询字符串,我发送到我的帐户管理服务,使用ocelot重新路由到我的代理服务。 这是我在代理服务中的Controller方法,用于接收重新路由的路径 [HttpGet] [Route(“GetPagedAgents”)] [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest)] public IActionResult Get(string page, string pageSize, string filter, string sortBy) { var Result = _agentServices.GetAll(Convert.ToInt32(page), Convert.ToInt32(pageSize),filter,sortBy); return Ok(Result); } 但它不起作用。 在我的OUTPUT窗口中显示消息:无法找到路径的下游路由:/ api / account / user […]

‘console’在asp.net 5控制台应用程序中不包含’ReadKey’的定义

我在VS2015 CTP的ASP.NET 5控制台中创建一个简单的应用程序。 对于以下代码行 // Wait for user input Console.ReadKey(); 我收到错误‘Console’不包含’ReadKey’的定义 。 此外,我得到一个建议作为ASP.Net 5.0-Available ASP.NET Core 5.0- Not available 。 不再使用ReadKey关键字? 这个建议意味着我需要添加一些参考?

WebHostBuilder。 如何在.NET-Core 2.0中使用CommandLine设置URL地址?

我使用下面的代码在我的.Net-Core 1.2环境中添加CommandLine,我可以使用命令行,如dotnet run –urls “http://a.cn.com:5000” –environment “Production” 。 public static void Main(string[] args) { var config = new ConfigurationBuilder() .AddJsonFile(“hosting.json”, optional: true) .AddCommandLine(args) .Build(); var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup() .UseConfiguration(config) .Build(); host.Run(); } 迁移到.net核心2.0之后就变成了这样 public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) { return WebHost.CreateDefaultBuilder(args) .ConfigureLogging((context, […]

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 […]

ASP.NET Core 1.0 POST IEnumerable 到控制器

我有一个动作,返回一个模型到视图IEnumerable 。 在视图中,我使用foreach遍历列表。 T类型具有名为Amount的属性。 现在,当我单击SAVE按钮时,我想将模型(IEnumerable)发布到一个动作。 IEnumerbale项目,其属性Amount应包含正确的值。 当我提交它时,在动作中,模型为空。 为了测试, IEnumerable是IEnumerable public class Product { public string Title { get; set; } public int Amount { get; set; } } 查看显示产品: @model IEnumerable @foreach (var product in Model) { @product.Title } SAVE 控制器后期行动: [HttpPost] public async Task Order(IEnumerable model) { }

如何使用Web API Get方法返回图像

我需要使用Web API Get方法返回一个图像。 下面的代码似乎工作正常,除了我在Fiddler的ImageView窗口中收到此消息,“此响应已编码,但并未声称是图像。” public HttpResponseMessage Get() { using (FileStream fs = new FileStream(filePath, FileMode.Open)) { HttpResponseMessage response = new HttpResponseMessage(); response.Content = new StreamContent(fs); response.Content.Headers.ContentType = new MediaTypeHeaderValue(“image/jpeg”); return response; } } 我在Fiddler中也看到了与此代码相同的结果: public HttpResponseMessage Get() { HttpResponseMessage response = new HttpResponseMessage(); Byte[] b = (GetImageByteArray()); response.Content = new ByteArrayContent(b); response.Content.LoadIntoBufferAsync(b.Length).Wait(); response.Content.Headers.ContentType = new […]

如何在动作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(); } }

AppSettings.json用于ASP.NET核心中的集成测试

我正在遵循本指南 。 我在API项目中有一个使用appsettings.json配置文件的appsettings.json 。 public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile(“appsettings.json”, optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .ReadFrom.Configuration(Configuration) .CreateLogger(); } 我正在看的特定部分是env.ContentRootPath 。 我做了一些挖掘,看起来我的appsettings.json实际上并没有复制到bin文件夹,但是这很好,因为ContentRootPath返回了MySolution\src\MyProject.Api\ ,这是appsettings.json文件所在的位置。 所以在我的集成测试项目中,我有这个测试: public class TestShould { private readonly TestServer _server; private readonly HttpClient _client; public TestShould() { […]