简体   繁体   English

C#WebApi-ASP Net Core 2-如何以编程方式将csproj名称放入路由?

[英]C# WebApi - Asp Net Core 2 - How can I put the csproj name into the route programmatically?

Edit: To be clear, I dont actually care if its the csproj. 编辑:明确地说,我实际上不在乎它是否是csproj。 I just want to be able to globally define a root name for all my routes. 我只想能够为所有路由全局定义一个根名称。

I am new to dotnet core 2 and c# in general. 一般来说,我是dotnet core 2和c#的新手。 I am building a webapi template for the rest of my team to use, but I want to be able to put the csproj name as part of the route so people dont have to change it in every controller. 我正在构建一个Webapi模板供团队其他成员使用,但是我希望能够将csproj名称作为路由的一部分,这样人们不必在每个控制器中都进行更改。

So for example: 因此,例如:

WebStoreAbc.csproj
--------

[Route("api/v1/#CSPROJNAME#/products")]
public class ProductsController : Controller
{
  ... controller logic ...
}


Registers at runtime as:
/api/v1/WebStoreAbc/products

This way, I dont have to configure routes for a bunch of controllers everytime I make a new project from the template. 这样,每次从模板创建新项目时,我都不必为一堆控制器配置路由。

Edit 2: I used Mahmoud Heretani's answer below and tweaked it. 编辑2:我在下面使用了Mahmoud Heretani的答案并对其进行了调整。

string projectName = "template";

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "api/v1/" + projectName.ToLower() + "/{controller=Home}/{action=Index}/{id?}");
    });
}

It think that you cannot insert a variable inside [Route] attribute, because it required a constant value, however a simple trick inside your Configure method should do the trick: 它认为您不能在[Route]属性内插入变量,因为它需要一个恒定值,但是Configure方法内的一个简单技巧可以解决这个问题:

 var projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "api/v1/" + projectName + "/{controller=Home}/{action=Index}/{id?}");
        });

But in your case, I would recommend that you create an assembly (class library) contains a method to configure the route using the assembly name (just like the previous example), and then use this method across your projects. 但是对于您而言,我建议您创建一个程序集(类库),该程序集包含一个使用程序集名称配置路由的方法(就像前面的示例一样),然后在您的项目中使用此方法。

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

相关问题 ASP.NET Core WebAPI 2 PUT方法名称 - ASP.NET Core WebAPI 2 PUT method name 如何使用asp.net mvc和razor模板引擎从c#中获取路由名称的URL? - How can I get the URL from route's name in c# using asp.net mvc and the razor template engine? 如何将对象从C#WebApp发送到ASP.NET WebAPI - how can I send object from c# webapp to asp.net webapi 如何对从C#控制台应用程序使用表单身份验证的ASP.NET WebAPI进行身份验证? - How can I authenticate to an ASP.NET WebAPI that is using Forms Authentication From a C# Console Application? 在 C# ASP.NET Core 3.1 应用程序中,我如何路由到 controller 然后查询字符串? - In a C# ASP.NET Core 3.1 application how do I route to a controller then query string? 如何使用 ASP.NET Core 获取当前路由名称? - How can I get the current route name with ASP.NET Core? 如何在webapi c#中配置路由,以便可以使用任何变量名代替{id} - how to configure route in webapi c#, so that any variable name can be used in place of {id} 使用ASP.NET WebApi,如何使路由排除一组控制器? - Using ASP.NET WebApi, how can I make a route exclude a set of controllers? 如何使用C#以编程方式运行ASP.Net开发服务器? - How can I programmatically run the ASP.Net Development Server using C#? 我可以将 gRPC 和 webapi 应用程序组合到 C# 中的 .NET Core 3.0 中吗? - Can I combine a gRPC and webapi app into a .NET Core 3.0 in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM