Tag: asp.net core mvc

在第二级包含几个参考

假设我们有这个模型: public class Tiers { public List<Contact> Contacts { get; set; } } 和 public class Contact { public int Id { get; set; } public Tiers Tiers { get; set; } public Titre Titre { get; set; } public TypeContact TypeContact { get; set; } public Langue Langue { get; set; } public Fonction […]

ASP.NET 5 / MVC 6相当于HttpException

在MVC 5中,您可以使用HTTP代码来引发HttpException,并将其设置为如下所示: throw new HttpException((int)HttpStatusCode.BadRequest, "Bad Request."); HttpException在ASP.NET 5 / MVC 6中不存在。什么是等效代码?

如何编译保存在Visual Studio 2015中的.less文件(预览)

好的,所以我在Visual Studio 2015 Preview中创build了一个新的ASP.Net 5 / MVC 6项目。 按照我们目前的做法,为了造型我想使用.less文件。 创build文件很简单,但Web Essentials不再编译它们。 所以我的问题是这样的:当我保存.less文件时,我需要做些什么才能使我的.css文件生成? 根据我的冒险得到Typescript很好地工作,我将不得不使用Grunt来完成这项任务,但我是Grunt全新的,所以我不知道该怎么做呢? 请帮忙!

ASP.NET核心中的OAuth授权服务

在Web API 2中,您曾经能够通过使用如下的中间件设置OAuth授权服务器来创build端点来颁发令牌: //Set up our auth server options. var OAuthServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new PathString("/token"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), Provider = new SimpleAuthorizationServerProvider() }; // Sets up the token issue endpoint using the options above app.UseOAuthAuthorizationServer(OAuthServerOptions); 也许我错过了,但我想弄清楚如何在ASP.NET核心中做到这一点。 我查看了源代码( https://github.com/aspnet/Security ),但是我没有看到任何类似的东西。 有没有新的方法来完成这个? 我需要创build一个控制器,并自己做? 我看到如何通过中间件设置OAuth身份validation,但是这涉及到我从API发出声明的授权部分。

如何从ASP.NET Core RC2 Web Api返回HTTP 500?

回到RC1,我会这样做: [HttpPost] public IActionResult Post([FromBody]string something) { … try{ } catch(Exception e) { return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError); } } 在RC2中,不再有HttpStatusCodeResult,也没有什么我能find的,让我返回一个500types的IActionResult。 现在的方法与我所要求的完全不同吗? 我们不再试图捕捉Controller代码? 我们只是让框架抛出一个通用的500exception回API调用者? 对于开发,我怎么能看到确切的exception栈?

如何从控制器返回特定的状态码和内容?

我想要下面的示例控制器返回没有内容的状态代码418。 设置状态码非常简单,但是似乎需要做些事情来表明请求的结束。 在ASP.NET Core之前的MVC中,或者在可能是对Response.End()的调用的WebForms中,但是在ASP.NET Core中如何工作,其中Response.End不存在? public class ExampleController : Controller { [HttpGet][Route("/example/main")] public IActionResult Main() { this.HttpContext.Response.StatusCode = 418; // I'm a teapot // How to end the request?????? // I don't actually want to return a view but perhaps the next // line is required anyway? return View(); } }

如何在Asp.Net Core中注册同一个接口的多个实现?

我有从相同的接口派生的服务 public interface IService { } public class ServiceA : IService { } public class ServiceB : IService { } public class ServiceC : IService { } 通常像Unity这样的其他IOC容器允许你通过一些Key来注册具体的实现,区别它们。 在Asp.Net核心如何注册这些服务,并在运行时根据一些密钥解决它? 我没有看到任何Add服务方法采用通常用于区分具体实现的key或name参数。 public void ConfigureServices(IServiceCollection services) { // How do I register services here of the same interface } public MyController:Controller { public void DoSomeThing(string key) { […]

如何在ConfigureServices中获得开发/暂存/生产托pipe环境

如何在启动时在ConfigureServices方法中获得开发/暂存/生产托pipe环境? public void ConfigureServices(IServiceCollection services) { // Which environment are we running under? } ConfigureServices方法只需要一个IServiceCollection参数。

找不到与命令“dotnet-aspnet-codegenerator”匹配的可执行文件

当尝试使用Visual Studio 15 Enterprise with Update 3在ASP.NET Core项目中添加Controller时,出现以下错误: "The was an error running the selected code generator: No executables found matching command 'dotnet-aspnet-codegenerator'"

System.Net.Http vs Microsoft.Net.Http

我正在使用ASP.NET 5和MVC 6.我想使用HttpClient,但我注意到有两个NuGet包提供。 我使用哪一个? System.Net.Http Microsoft.Net.Http