简体   繁体   English

参数值之一为URL路径的路由

[英]Routing where one of parameter values is part of URL path

I am confused with routing in Asp.Net core 1 and I need help. 我对Asp.Net核心1中的路由感到困惑,需要帮助。 In startup.cs I have this configuration 在startup.cs中,我有此配置

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

I have created a controller 'Entities' and a method 'Get' 我创建了一个控制器“实体”和一个方法“获取”

[Authorize]
public class EntitiesController : Controller
{                    
    [Produces("text/html")]
    public string Get(string entity, string type)
    {            
        return "<html><body>test</body></html>";
    }

}

So, when I text in url link like below works 因此,当我在网址链接中输入以下内容时,

http://localhost:12895/Entities/Get?entity=entity&type=type

and function called with params. 和用params调用的函数。

But I want to change this url and keep same functionality. 但我想更改此网址并保持相同的功能。 I want my url become 我希望我的网址成为

http://localhost:12895/Entities/entity?type=type

so, only type will be parameter and the name of entity will change for example 因此,例如,只有type是参数,而entity的名称将改变

http://localhost:12895/Entities/human?type=type

http://localhost:12895/Entities/dog?type=type

but invoke the same function. 但调用相同的功能。

Is this possible? 这可能吗?

There's full info about .net core routings. 有关.net核心路由完整信息。

Yes. 是。 It is possible. 有可能的。 Add an additional route in app.UseMvc for your class. app.UseMvc为您的班级添加一条附加路线。

It should looks like a 它看起来像一个

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


    routes.MapRoute(
        name: "entities",
        template: "Entities/{entity}",
        defaults: new { controller = "Entities", action = "Get"}
    );
});

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

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