asp.net-core-mvc

Custom 404 response model

蹲街弑〆低调 提交于 2019-12-04 20:30:48
I want to provide a custom reponse for all 404s on our API. For example: { "message": "The requested resource does not exist. Please visit our documentation.." } I believe the following result filter works for all cases within the MVC pipeline: public class NotFoundResultFilter : ResultFilterAttribute { public override void OnResultExecuting(ResultExecutingContext context) { var result = context.Result as NotFoundResult; if (result != null) { context.Result = new HttpNotFoundResult(); // My custom 404 result object } } } But, when a URL requested does not match an action route, the above

Views not being copied when publish the project in netcore 2.0 visual studio 2017

允我心安 提交于 2019-12-04 20:06:30
问题 I have createa a ASP.NET Core 2.0 project in VS 2017. When I publish my project the Views folder are not there but the wwwroot folder is. This I can setup in my .csproj file with the following: <ItemGroup> <Content Update="appsettings.json;web.config" CopyToOutputDirectory="PreserveNewest"/> <Content Update="Views\**\*" CopyToOutputDirectory="PreserveNewest" /> <Content Update="wwwroot\**\*" CopyToOutputDirectory="PreserveNewest" /> </ItemGroup> but didn't work. 回答1: ASP.NET Core MVC has a

AddOAuth linkedin dotnet core 2.0

天涯浪子 提交于 2019-12-04 19:50:42
问题 I'm using dotnet core I want to setup a LinkedIn authentication on the site since there is no default authentication builder for LinkedIn as facebook, google and twitter I decided to use the generic implementation as follows: services.AddAuthentication().AddOAuth("LinkedIn", c => { c.ClientId = Configuration["linkedin-app-id"]; c.ClientSecret = Configuration["linkedin-app-secret"]; c.Scope.Add("r_basicprofile"); c.Scope.Add("r_emailaddress"); c.CallbackPath = "/signin-linkedin"; c

ASP.NET Core MVC model property binding is null

ぐ巨炮叔叔 提交于 2019-12-04 19:39:36
I am using ASP.NET Core RC2 MVC with Entity Framework and trying to save a new car. The problem is, that in the create method of the car controller the property Color is null when the action is posted back. All other properties/fields are set. But the Color which refers to the CarColors model is null. The CarColor model public class CarColor { [Key] public int CarColorId { get; set; } [MinLength(3)] public string Name { get; set; } [Required] public string ColorCode { get; set; } } The main model Car public class Car { [Key] public int CarId { get; set; } [MinLength(2)] public string Name {

Translating Ninject to ASP.NET MVC 6 DI

让人想犯罪 __ 提交于 2019-12-04 19:25:54
问题 I am trying to get into the new ASP.NET MVC 6 stuff, but I'm having a very hard time with their new DI system. I have tried to find resources online, but everything I find covers only the absolute most bare minimum to use it. I was previously using Ninject , and I have several wire-ups that work like this: Bind<IDocumentStore>() .ToMethod(c => CreateDocumentStore()) .InSingletonScope(); private static IDocumentStore CreateDocumentStore() { // lots of initialization code, etc. return

ASP.NET MVC 6 using IIS to develop without publish

别说谁变了你拦得住时间么 提交于 2019-12-04 19:02:47
Is there any way to develop an ASP.NET MVC 6 beta 8 application on IIS without having the need to publish the application? After install the httpPlatform module I am able to point IIS to a published application. But how can yo point IIS to say your visual studio solution wwwroot so you can develop and see the changes you make without having the need to publish? Is this no longer possible like how it you could develop asp.net MVC 5 apps? For development is IIS Express and Kestrel the preferred way now? Update: I've tried to run both commands like the vs.net debugger shows but I still get bad

Posting form data to MVC Core API

余生长醉 提交于 2019-12-04 18:36:41
问题 I would like to post data to my API using AJAX but I'm having issues. I'm using Fiddler to test my API and I'm able to post JSON correctly but when posting a name/value urlencoded string I get a 400 Bad Request with the response body being '{"":["The input was not valid."]}'. My debug window displays: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor:Information: Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.SerializableError'. The JSON being posted is: {

Special characters in Razor template not being encoded correctly

醉酒当歌 提交于 2019-12-04 17:47:04
问题 I make some test with ASP.NET Core MVC. I am trying to display special character like çùé. But it is displayed as �. Exemple : Create new view and put : <div>àçù</div> By default, the .cshtml file is encoded in "UTF-16". The charset in HTTP response header is "UTF-8". In _layout, the <head> contains the <meta charset="utf-8" /> tag. To resolve the problem, I can convert all .cshtml file to uft-8. What's the correct way to manage encodage in ASP MVC Core? Edit : Visual Studio 2017 RC create

How to get the current ASP.NET core controller method name inside the controller using Reflection or another accurate method

China☆狼群 提交于 2019-12-04 17:16:36
问题 I want to get the current method name of my ASP.NET Core controller I have tried getting the method name through reflection: [HttpGet] public async Task<IActionResult> CreateProcess(int catId) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; but this gives me a value of MoveNext and not CreateProcess Take note I don't want to use the ViewContext string methodName = ActionContext.RouteData.Values["action"].ToString(); as I lowercase my urls via the startup settings

Any way to use Authorization Policies in a view in .NET Core 1.0 MVC?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 17:11:25
问题 I know in controllers, you can write [Authorize("policyName")] without an issue, but is there any way to use a policy in a view? I'd rather not use User.IsInRole(...) every single time I want to authorize some HTML. Edit: Here's some code Startup.cs -- Policy Declaration services.AddAuthorization(options => { options.AddPolicy("testPolicy", policy => { policy.RequireAuthenticatedUser() .RequireRole("RoleOne", "RoleTwo", "RoleThree") .RequireClaim(ClaimTypes.Email); }); }); Admin Controller