asp.net-core-mvc

InvalidOperationException rendering ViewComponent in Strongly-Typed View

两盒软妹~` 提交于 2019-12-07 07:07:15
问题 Recently updated dotnet core 1.0.1 to 1.1 and ViewComponent in MVC starts failing with the below exception: InvalidOperationException: One or more errors occurred. (The model item passed into the ViewDataDictionary is of type 'App.Models.HomeViewModel', but this ViewDataDictionary instance requires a model item of type 'App.Components.LoginViewComponent'.) The Index.cshtml renders LoginViewComponent : @model App.Models.HomeViewModel <html> @Component.InvokeAsync("LoginViewComponent") </html>

ASP.NET Core ways to handle custom response/output format in Web API

别来无恙 提交于 2019-12-07 06:22:39
问题 I'd like to create custom JSON format, that would wrap the response in data and would return Content-Type like vnd.myapi+json Currently I have created like a wrapper classes that I return in my controllers but it would be nicer if that could be handled under the hood: public class ApiResult<TValue> { [JsonProperty("data")] public TValue Value { get; set; } [JsonExtensionData] public Dictionary<string, object> Metadata { get; } = new Dictionary<string, object>(); public ApiResult(TValue value)

Scaffolding in .Net Core with Multiple Projects in Solution

痞子三分冷 提交于 2019-12-07 06:05:48
问题 I have created an .Net Core MVC6 application targeting net461. I have used a project structure I am very familiar with in which I place the data, model, and service classes in separate class library projects and the Web project references these. When I attempt to scaffold a controller I receive an error that multiple matching types exist for the model I am scaffolding. If I move all code to a single project, scaffolding is successful. If I move the context to Web project and leave the model

No response received with JsonResult in MVC 6

早过忘川 提交于 2019-12-07 05:47:56
问题 I'm using ASP.NET MVC 6 in beta6. In my Startup.cs I have the following code: services.AddMvc().Configure<MvcOptions>(o => { o.OutputFormatters.RemoveAll(formatter => formatter.GetType() == typeof(JsonOutputFormatter)); var jsonOutputFormatter = new JsonOutputFormatter { SerializerSettings = { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore } }; o.OutputFormatters.Insert(0, jsonOutputFormatter); }); In my controller I have: public async Task<JsonResult> Get() { var result

Is there any way to get request body in .NET Core FilterAttribute?

左心房为你撑大大i 提交于 2019-12-07 05:30:46
问题 Sample of my request http://localhost:8065/api/note POST content-type:application/json request body: { "id" : "1234", "title" : "test", "status" : "draft"} and the response should be { "msg" : "ok", "code" : 1} The action public async Task<IActionResult> Post([FromBody]NoteModel model) In order to have every request logged automatically, I create an attribute to do this job. The attribute looks like: (from Microsoft Docs) public class SampleActionFilterAttribute : TypeFilterAttribute { public

WebTelemetryInitializerBase in ASP.NET Core / MVC6

江枫思渺然 提交于 2019-12-07 05:09:26
问题 Is there an MVC6 compatible version of WebTelemetryInitializerBase that would work with ASP.NET Core (on the full .NET Framework)? See my question here where I asked how to get HttpContext in my temeletry initializers. Unfortunately I didn't specify that I was using MVC 6 and thus no System.Web.HttpContext . 回答1: Yes, there is a version of this for aspnetcore. Check out the Microsoft Application Insights for ASP.NET Core applications repo. There is an implementation of getting the WebUser

Redirect Login to Controller Action

妖精的绣舞 提交于 2019-12-07 05:02:37
问题 Starting with the ASP.NET 5 Web App Template using Individual User Accounts I have managed to get external authentication working with Microsoft accounts. When users click Login they are redirected to ExternalLogin in AccountController like this <form asp-controller="Account" asp-action="ExternalLogin" method="post" asp-route-returnurl="@ViewData["ReturnUrl"]" class="nav navbar-right"> <button type="submit" class="btn btn-null nav navbar-nav navbar-right" name="provider" value="Microsoft"

.NET core custom and default binding combined

主宰稳场 提交于 2019-12-07 04:00:48
问题 I'm creating a custom model binder for a view model, implementing IModelBinder I have a lot of properties in my view model, the majority of which do not need any custom binding. Rather than explicitly set all of the property values on my model individually from the ModelBindingContext , I would to be able to get the framework to bind the model for me, then I would carry out any custom binding: public class ApplicationViewModelBinder : IModelBinder { public Task BindModelAsync

How to define function that returns html in asp.net core

∥☆過路亽.° 提交于 2019-12-07 03:50:49
问题 Basically I need something like old asp.net @helper MakeNote(string content) { <p><strong>Note</strong>   @content </p> } or JSX MakeNote(note) { return (<div>Note {note}</div>); } A partial view is not an option. I am happy with either a function returning an IHtmlString, or a function writing to the underlying writer. It also needs to support Razor Syntax (not just string concatenation) inside the function. 回答1: You might be looking for @functions that use Html.Raw. Here is an example that

How to not get “ERROR_FILE_IN_USE ” error when deploying a ASP.NET Core website using FTP/shared folder?

£可爱£侵袭症+ 提交于 2019-12-07 03:47:55
问题 I have an ASP.NET Core RTM Web Application that targets .net framework 4.52 In my project.json "frameworks": { "net452": {} }, When I publish it to an folder it creates a MyWebApplication.exe and when I access the app in my browser the exe is run. I need to publish the website to a web server that has Windows Server 2012 and IIS8 I have shared folder access via VPN to the website deploy directory. The first time I publish the site to the folder it worked correctly. I access the website url