asp.net-core-mvc

How can I have MVC 6 return 405 Method Not Allowed instead of 404 Not Found?

孤街醉人 提交于 2019-12-10 18:45:43
问题 It appears that MVC 6 does not return 405 Method Not Allowed responses when a route matches except for the verb. This is, in my opinion, a regression from Web API 2 because it's not as RESTful. Is there an easy way to change this behavior? 回答1: This is not possible with MVC 6's current implementation. A sad oversight, and one that, along with other abstraction failures on Microsoft's part, drove my team and I to use NancyFx. https://github.com/aspnet/Mvc/issues/388 来源: https://stackoverflow

There is already an object named AspNetRoles in the database. (entity-framework-core)

瘦欲@ 提交于 2019-12-10 18:43:15
问题 I have an asp.net core mvc website using entity framework core. I don't know how it initially happened, but I can't get past the error: "There is already an object named AspNetRoles in the database" I ended up deleted the database, deleting all my migration .cs files and starting from scratch. I've then tried add-migration MyInitialMigration same error I then deleted the database again, and tried a direct update-database (without adding a migration) still the same error I tried changing the

GetExternalLoginInfoAsync returns null dotnet core 2.0

…衆ロ難τιáo~ 提交于 2019-12-10 18:36:24
问题 I'm trying to setup Facebook authentication with dot-net core 2.0, but in my ExternalLoginCallbackAsync method, I'm always getting null as a response I have followed the documentation and so far this is what I've done: in my ConfigureServices in the startup file: services.AddAuthentication( options => options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme); services.AddAuthentication().AddFacebook( f => { f.AppId = Configuration["facebook-app-id"]; f.AppSecret =

How to pass a URL as a query string parameter in MVC

十年热恋 提交于 2019-12-10 18:32:19
问题 does anyone know how to pass a URL as a query string parameter and then get the URl in HttpGet method as a parameter ? 回答1: Thanks so much for all the answers. Finally I got it sorted. Please refer to my fix below: [Route("api/[controller]")] public class UrlController : Controller { [HttpGet("{*longUrl}")] public string ShortUrl(string longUrl) { var test = longUrl + Request.QueryString; return JsonConvert.SerializeObject(GetUrlToken(test)); } 回答2: just like this? <a href="/Home/Index?url=

An unhandled exception has occurred: Cannot find compilation library location for package 'Microsoft.Win32.Registry'

☆樱花仙子☆ 提交于 2019-12-10 18:29:36
问题 I'm using asp.net core 2.0 when i build to my web project for docker and run it as a container its throw an exception as below; but no any problem when i run it on local. I did basically, before then docker build; dotnet restore dotnet build dotnet publish fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0] An unhandled exception has occurred: Cannot find compilation library location for package ' Microsoft.Win32.Registry ' System.InvalidOperationException: Cannot find

Is it possible to set radio button tag helper value attribute as “checked” in html?

孤人 提交于 2019-12-10 18:29:00
问题 I was looking for a way to use the value attribute on radio button tag helper to inform if the button was checked or not, instead of using a separate field for selection. I found an answer by @Shyju to a related question. I am using a modified example of that answer to show what I mean. Let's say I have a viewmodel public class ExampleViewModel { public int Id { get; set; } public string UserName { get; set; } public List<UserRole> Roles { get; set; } } public class UserRole { public int Id {

IN MVC6 return Json(rows, JsonRequestBehavior.AllowGet) ISSUE

◇◆丶佛笑我妖孽 提交于 2019-12-10 18:21:18
问题 IN MVC6 return Json(rows, JsonRequestBehavior.AllowGet); method is changed and not allowing to set JsonrequestBehavior. What is alternative in MVC6 回答1: That overload of Json method which takes JsonRequestBehavior does not exist in the aspnet core any more. You can simply call the Json method with the object data you want to send back. public IActionResult GetJsonData() { var rows = new List<string> { "Item 1","Item 2" }; return Json(rows); } Or even public IList<string> GetJsonData() { var

MVC6 Cors - intercept preflight

*爱你&永不变心* 提交于 2019-12-10 17:42:45
问题 I'm upgrading my WebApi(s) to MVC6. In WebApi I could intercept every HTTP request and if it was a preflight I could respond with headers the browser would accept. I'm trying to figure out how to do the same thing in MVC6 WebApi. Here is the WebApi code. protected void Application_BeginRequest(object sender, EventArgs e) { if (Context.Request.Path.Contains("api/") && Context.Request.HttpMethod == "OPTIONS") { Context.Response.AddHeader("Access-Control-Allow-Origin", Context.Request.Headers[

Access model data in AddValidation method asp.net core custom validation

*爱你&永不变心* 提交于 2019-12-10 17:34:50
问题 I am following this example: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation and trying to implement my own custom attribute for validation. Now, the viewmodel has two fields that I wish to access from inside this method, so that they can be rendered with the "data-val" attributes. My question is, how can I get say a property called "Myprop" from context here? When I debug I can se the information under context.ActionContext.ViewData.Model but I have no way of getting that

Remotely publish ASP.NET 5 to IIS

ⅰ亾dé卋堺 提交于 2019-12-10 17:27:24
问题 I've got an ASP.NET 5 project that I'd like to deploy to a remote server. I don't have remote desktop access to this machine, otherwise I could follow the same method most people use when deploying to IIS. I'm able to deploy to my IIS 8.5 just fine on the same machine as my development. First I package my project to get the approot and wwwroot directories. Then, I point IIS to my wwwroot, and all works fine. The problem is trying to deploy remotely. Normally I would zip up my MVC 5 project