asp.net-core-mvc

What is difference between ObjectResult and JsonResult

走远了吗. 提交于 2019-12-09 00:20:59
问题 There are two classes in Microsoft.AspNetCore.Mvc namespace: ObjectResult and JsonResult . Both convert the returned object in the JSON format. What is difference between them and what is the purpose to use them? 回答1: JsonResult is an IActionResult which formats the given object as JSON ObjectResult is an IActionResult that has content negotiation built in. Inside its ExecuteResultAsync , responsible for writing to the response stream, the framework will walk through the available formatters

MVC 6 Tag Helpers and foreach

感情迁移 提交于 2019-12-08 23:17:52
问题 What would I give to asp-for property of a label tag helper in order to display items from a collection. The code below generates a compilation error. @foreach (var item in Model) { <label asp-for="item.BookingCode"></label> } 回答1: The @ character escapes the default model lambda code. Therefore you can type: @foreach (var item in Model) { <label asp-for="@item.BookingCode"></label> } 回答2: I Have a simple way to do a list and show properties of it. List<string> razones = new List<string>();

No authentication handler is configured to handle the scheme: Automatic

浪尽此生 提交于 2019-12-08 23:05:16
问题 I updated ASP.NET 5 framework beta-8 packages with RC ones on previously working application. After I got it running next error occured in the startup process: InvalidOperationException: No authentication handler is configured to handle the scheme: Automatic Microsoft.AspNet.Http.Authentication.Internal.DefaultAuthenticationManager.d__12.MoveNext() var defaultPolicy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); services.AddMvc(setup => { setup.Filters.Add(new

Cant hit breakpoint in web api controller

非 Y 不嫁゛ 提交于 2019-12-08 20:23:21
问题 When i try to debug this code : // POST: api/Events [HttpPost] public async Task<IActionResult> PostEvent([FromBody] object savedEvent) { Event addedEvent = JsonConvert.DeserializeObject<Event>(savedEvent.ToString()); if (!ModelState.IsValid) { return BadRequest(ModelState); } Cant hit this line : Event addedEvent = JsonConvert.DeserializeObject<Event>(savedEvent.ToString()); Debuger reacts like i hit continue but code past doesnt execute. Im really confused. Thanks for your help. 回答1: Try

ASP.NET Core map route to static file handler

戏子无情 提交于 2019-12-08 19:42:15
问题 I'm working on a ASP.NET Core website (previously named ASP.NET 5 / vNext) with Angular. In order for Angular to work I need to have a catch-all route: app.UseStaticFiles(); app.UseMvc(routes => { // Angular fallback route routes.MapRoute("angular", "{*url}", new { controller = "Home", action = "Index" }); }); I also have a few files/folders in wwwroot, like: wwwroot/app wwwroot/assets wwwroot/lib When any requests are made to these paths, for example http://example.com/assets/css/test.css ,

Pass parameters to a Requirement/Policy in ASP.NET MVC 6

馋奶兔 提交于 2019-12-08 19:16:38
问题 I was trying to make a custom authorization attribute in ASP.NET vNext, until I found this excelent answer from @blowdart in this post: https://stackoverflow.com/a/31465227/1756978 indicating that Authorization requirements is now the way to go. The answer is very clarifying but doesn't indicates how to pass a parameter to this requirements / policies. What I'm trying to do is porting a MVC 5 custom authorization attribute which has this signature: [Autorizacion(Requires = enumPermission

asp.net core JWT in uri query parameter?

牧云@^-^@ 提交于 2019-12-08 19:07:20
问题 I have an api that is protected by JWT and Authorize attribute and at the client I use jquery ajax call to deal with it. This works fine, however I now need to be able to secure downloading of files so I can't set a header Bearer value, can it be done in the URI as an url parameter? =-=-=-=- UPDATE: This is what I ended up doing for my scenario which is an in-house project and very low volume but security is important and it might need to scale in future: When user logs in I generate a random

How to get custom attributes for a controller in asp.net core rc2

纵饮孤独 提交于 2019-12-08 16:14:25
问题 I have created a custom attribute : [AttributeUsage(AttributeTargets.Method| AttributeTargets.Class)] public class ActionAttribute : ActionFilterAttribute { public int Id { get; set; } public string Work { get; set; } } my controller : [Area("Administrator")] [Action(Id = 100, Work = "Test")] public class HomeController : Controller { public IActionResult Index() { return View(); } } my code : i use reflection to find all Controllers in the current assembly Assembly.GetEntryAssembly()

Failed to Authenticate HTTPS connection when attempting GET from WebApi

倾然丶 夕夏残阳落幕 提交于 2019-12-08 15:53:19
问题 I am using ASP.Net Core and I have 2 projects. ASP.Net Core MVC application ASP.Net Core WebApi application If i attempt one of the WebApi end points using Postman i do not have any issues and the /api/values returns as expected (standard test endpoint) However if i attempt the same operation using the MVC application I get a very frustrating error: HttpsConnectionFilter[1] Failed to authenticate HTTPS connection I am hosting using Kestrel for Asp.Net core. I have a self signed PFX

Kestrel on AspNet vNext doesnt serve index page under /

谁都会走 提交于 2019-12-08 15:43:53
问题 I need to be able to serve my 'index.html', under the default url / , using Kestrel web server. Right now I'm only able to access my static files with the full path i.e /index.html Again this works perfectly on VisualStudio, the context is OSX with Kestrel This is my Startup.cs public void ConfigureServices(DI.IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app) { app.UseStaticFiles(); app.UseMvc(); } The solution I have so far, is to do a