是否可以在没有AddMvc的ASP.Net Core 2.1中创建路由映射?

时间:2018-09-18 01:26:24

标签: asp.net-mvc asp.net-core

我对ASP.NET Core完全陌生,我进行了很多搜索,但仍然对Core 2.1中的路由感到困惑。

因此,我创建了一个示例项目选择API作为模板,而VS创建了如下所示的内容:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseMvc();

    }

但是我不需要MVC提供的所有功能,因为我的项目不使用视图。

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:3)

是的。尽管我们经常将路由与MVC结合使用,但是Routing是一个与MVC无关的项目。

与ASP.NET Core一起使用时,路由在后台充当RouterMiddleware。如果您不想使用MVC,只需构建一个路由器:

private IRouter BuildRouter(IApplicationBuilder applicationBuilder)
{
    var builder = new RouteBuilder(applicationBuilder);

    // use middlewares to configure a route
    builder.MapMiddlewareGet("/api/hello", appBuilder => {
        appBuilder.Use(async (context,next) => {
            context.Response.Headers["H1"] = "Hello1";
            await next();
        });
        appBuilder.Use(async (context,next) => {
            context.Response.Headers["H2"] = "Hello2";
            await next();
        });
        appBuilder.Run(async (context) => {
            await context.Response.WriteAsync("Hello,world");
        });

    });

    builder.MapMiddlewarePost("/api/hello", appBuilder => {
        // ...
    });

    // ....

    return builder.Build();
}

并注册路由器中间件

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // ...
    app.UseRouter(BuildRouter(app));
}

这是工作时的屏幕截图:

enter image description here

答案 1 :(得分:1)

是的。来自https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.1

var routeBuilder = new RouteBuilder(app, trackPackageRouteHandler);

routeBuilder.MapGet("hello/{name}", context => {
    var name = context.GetRouteValue("name");
    return context.Response.WriteAsync($"Hi, {name}!"); });            

var routes = routeBuilder.Build(); app.UseRouter(routes);

或者,如果您要将其实现为自定义中间件,并且只需要基本路由:

发件人:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.1

public class Startup
{
    private static void HandleMapTest1(IApplicationBuilder app)
    {
        app.Run(async context =>
        {
            await context.Response.WriteAsync("Map Test 1");
        });
    }

    public void Configure(IApplicationBuilder app)
    {
        app.Map("/map1", HandleMapTest1);

        app.Run(async context =>
        {
            await context.Response.WriteAsync("Hello from non-Map delegate. <p>");
        });
    }
}

或者,如果您需要更多的路由功能,请参见itmius的答案https://stackoverflow.com/a/52377807/2085502

答案 2 :(得分:0)

已解决,请使用以下启动程序:

public void ConfigureServices(IServiceCollection services)
{
     services.AddMvcCore().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddApiExplorer()
            .AddAuthorization()
            .AddJsonFormatters()
            .AddCors();
}

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseMvc();
}

感谢所有试图帮助我的人!