asp.net-core-mvc

ASP.NET Core HTTPRequestMessage returns strange JSON message

假如想象 提交于 2019-12-17 16:56:48
问题 I am currently working with ASP.NET Core RC2 and I am running into some strange results. So I have an MVC controller with the following function: public HttpResponseMessage Tunnel() { var message = new HttpResponseMessage(HttpStatusCode.OK); message.Content = new StringContent("blablabla", Encoding.UTF8); message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain"); message.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue {

Nesting TagHelpers in ASP.NET Core MVC

*爱你&永不变心* 提交于 2019-12-17 16:35:24
问题 The ASP.NET Core TagHelper documentation gives the following example: public class WebsiteContext { public Version Version { get; set; } public int CopyrightYear { get; set; } public bool Approved { get; set; } public int TagsToShow { get; set; } } [TargetElement("website-information")] public class WebsiteInformationTagHelper : TagHelper { public WebsiteContext Info { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { output.TagName = "section";

Read request body twice

廉价感情. 提交于 2019-12-17 16:34:20
问题 I am trying to read the body in a middleware for authentication purposes, but when the request gets to the api controller the object is empty as the body has already been read. Is there anyway around this. I am reading the body like this in my middleware. var buffer = new byte[ Convert.ToInt32( context.Request.ContentLength ) ]; await context.Request.Body.ReadAsync( buffer, 0, buffer.Length ); var body = Encoding.UTF8.GetString( buffer ); 回答1: If you're using application/x-www-form-urlencoded

How to mock an IFormFile for a unit/integration test in ASP.NET Core 1 MVC 6?

不羁岁月 提交于 2019-12-17 16:32:28
问题 I want to write tests for uploading of files in ASP.NET Core 1 but can't seem to find a nice way to mock/instanciate an object derived from IFormFile. Any suggestions on how to do this? Thanks. 回答1: Assuming you have a Controller like.. public class MyController : Controller { public Task<IActionResult> UploadSingle(IFormFile file) {...} } ...where the IFormFile.OpenReadStream() is accessed with the method under test. You can create a test using Moq mocking framework to simulate the stream

Unable to utilize UrlHelper

会有一股神秘感。 提交于 2019-12-17 16:30:24
问题 I'm currently trying to do something that was dead simple and straight forward in ASP.NET 4 however this ins't the case now in ASP.NET 5. Previously to use the UrlHelper it was dead simple: var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); However I can't for the life of me wrap my head around how to use the new UrlHelper. I'm looking at the test cases and either I'm completely daft or I'm missing something and I can't seem to figure it out. Any help here in clearing

How to ignore routes in ASP.NET Core 1.0.1?

一个人想着一个人 提交于 2019-12-17 16:27:44
问题 Previously, one would add something like this to Global.aspx.cs , which is gone in .NET Core: routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); Here's what I currently have in my Startup.cs (for .NET Core): app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); routes.MapSpaFallbackRoute( name: "spa-fallback", defaults: new { controller = "Home", action = "Index" }

Using EF Core ThenInclude() on Junction tables

风流意气都作罢 提交于 2019-12-17 16:27:11
问题 I'm transfering my .NET Framework (EF6) code to ASP.NET Core (EF Core), and I stumbled upon this issue. Here is some example code: In EF6 I use Include() and Select() for eager-loading: return _context.Post .Include(p => p.PostAuthor.Select(pa => pa.Author).Select(a => a.Interests)) PostAuthor is a junction table and there is also a Junction table "AuthorInterest" which I didn't need to involve in EF6 (Select goes straight to a.Interests). Anyway, I can see that in EF7 this is reworked,

Custom scaffold templates in ASP.NET Core

廉价感情. 提交于 2019-12-17 15:27:53
问题 Now that I figured out how to enable scaffolding in ASP.NET Core MVC (see View scaffold templates in ASP.NET Core), I'd like to create custom template files for Views. I found one place that said the template files are located here: C:\Users\{user name}\.dnx\packages\Microsoft.Extensions.CodeGenerators.Mvc\1.0.0-rc1-final\Templates\ViewGenerator But I copied an existing file there and that copied file does not appear in the Template dropdown on the Add View dialog. Are they located elsewhere,

Custom scaffold templates in ASP.NET Core

和自甴很熟 提交于 2019-12-17 15:26:03
问题 Now that I figured out how to enable scaffolding in ASP.NET Core MVC (see View scaffold templates in ASP.NET Core), I'd like to create custom template files for Views. I found one place that said the template files are located here: C:\Users\{user name}\.dnx\packages\Microsoft.Extensions.CodeGenerators.Mvc\1.0.0-rc1-final\Templates\ViewGenerator But I copied an existing file there and that copied file does not appear in the Template dropdown on the Add View dialog. Are they located elsewhere,

MVC 6 EF7 RC1 creating multiple dbcontexts

試著忘記壹切 提交于 2019-12-17 12:37:41
问题 I am trying to figure out how to create a second DB context in EF7 RC1. In the past I could use a constructor with :base("connectionName") but that no longer seems an option since it says cannot convert string to System.IServiceProvider. My second context code is as follows: public class DecAppContext : DbContext { public DecAppContext() // :base("DefaultConnection") { } public DbSet<VignetteModels> VignetteModels { get; set; } public DbSet<VignetteResult> Result { get; set; } } } In my