简体   繁体   English

没有MVC的.Net Core 2路由

[英].Net Core 2 Routing without MVC

I am trying to create a simple .Net Core 2 application with an Angular front end and an api/db backend. 我正在尝试使用Angular前端和api / db后端创建一个简单的.Net Core 2应用程序。

I do not want to use MVC, since I have no need for typical dot net Views or Models, but I am having trouble serving my index.html files and also routing the api calls to my controller. 我不想使用MVC,因为我不需要典型的点网视图或模型,但我在服务我的index.html文件时遇到问题,并且还将api调用路由到我的控制器。

I know it's somewhere in the program.cs and involving the routes, but I have been having trouble finding documentation on how to do what I want without using the mvc package. 我知道它在program.cs中的某个地方并且涉及到路由,但是我一直无法在不使用mvc包的情况下找到关于如何做我想做的文档。

Simply, here are my requirements again: 简单地说,这是我的要求:

  1. Serve my angular index.html file when routing to the root (let angular handle the rest of the routes there) 路由到根时提供我的angular index.html文件(让angular处理其余的路由)
    1. Route my /api routes to be routed to ac# dot net controller 将my / api路由路由到ac#dot net controller
    2. Not to use MVC 不要使用MVC

Thanks! 谢谢!

You can use the the UseRouter() middleware to explicitly map routes directly as part of your application configuration pipeline. 您可以使用UseRouter()中间件直接显示路由,作为应用程序配置管道的一部分。

For example, the following creates a custom route to handle Lets Encrypt requests which by default would fail if any other routing is enabled. 例如,以下内容创建了一个自定义路由来处理Lets Encrypt请求,如果启用了任何其他路由,默认情况下这些请求将失败。 The following code goes into the Configure() method of the server's Startup class: 以下代码进入服务器的Startup类的Configure()方法:

        // Handle Lets Encrypt Route(before MVC processing!)
        app.UseRouter(r =>
        {
            r.MapGet(".well-known/acme-challenge/{id}", async (request, response, routeData) =>
            {
                var id = routeData.Values["id"] as string;
                var file = Path.Combine(env.WebRootPath, ".well-known", "acme-challenge", id);
                await response.SendFileAsync(file);
            });
        });

This essentialy makes it very easy to simple routing scenarios or forward requests to other handlers. 这实际上使得简单的路由方案或向其他处理程序转发请求变得非常容易。

Note that this is a raw interface that doesn't include any input or output handling beyond the raw Request/Response semantics. 请注意,这是一个原始接口 ,不包括原始请求/响应语义之外的任何输入或输出处理。

If you are doing anything with API style data, then I would still recommend using MVC and controllers which handle all sorts of things that you'd otherwise have to build yourself. 如果您正在使用API​​样式数据做任何事情,那么我仍然建议使用MVC和控制器来处理您自己必须自己构建的各种事物。 In Core, API and MVC run the same Controller pipeline so you can think of MVC as Web API and MVC combined. 在Core,API和MVC中运行相同的Controller管道,因此您可以将MVC视为Web API和MVC的组合。

For most common use cases using MVC is still the way to go. 对于大多数使用MVC的常见用例仍然是可行的方法。 The above approach is great for micro-services or one off requests as that might buy you a little bit of a performance gain since it doesn't have to load any of the MVC bits or fire into that pipeline, but you're responsible for doing your own serialization and request processing. 上述方法对于微服务或一次性请求非常有用,因为它可能会为您带来一些性能提升,因为它不需要加载任何MVC位或触发该管道,但您需要负责做自己的序列化和请求处理。

As an alternative, you can always activate MVC using .AddMvcCore() and turn on the JSON formatter (I'm assuming your API will be JSON) using .AddJsonFormatters() . 作为替代方案,您始终可以使用.AddMvcCore()激活MVC并使用.AddMvcCore()打开JSON格式化程序(我假设您的API将是JSON .AddJsonFormatters()

In this scenario, you'll inherit from ControllerBase instead of Controller . 在这种情况下,您将从ControllerBase而不是Controller继承。

Provided you have static files enabled, you can just place the index.html in the top-level of your webroot ( wwwroot ) and it will be served as the default document. 如果您启用了静态文件,则只需将index.html放在webroot( wwwroot )的顶层,它就会作为默认文档。 Your Angular app will take over from their, interoping with the API you've built on the backend. 您的Angular应用程序将接管它们,并使用您在后端构建的API进行干预。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM