Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将url重定向到特定端点_C#_Asp.net_Asp.net Mvc_Api_Controller - Fatal编程技术网

C# 如何将url重定向到特定端点

C# 如何将url重定向到特定端点,c#,asp.net,asp.net-mvc,api,controller,C#,Asp.net,Asp.net Mvc,Api,Controller,我需要在web项目中添加一个api端点(=>WebSiteUrl/api/token) 问题是这个项目已经有了一个名为“ApiController”的基本控制器,具有多个操作。我无法使用此控制器,因为它是一个基本控制器,而不是ApiController 尽管已经有一个控制器正在使用路由WebSiteUrl/api/actions,但是否有方法设置WebSiteUrl/api/token端点 我已经创建了一个apicontroller,它使用以下路径:WebSiteUrl/api/controll

我需要在web项目中添加一个api端点(=>WebSiteUrl/api/token)

问题是这个项目已经有了一个名为“ApiController”的基本控制器,具有多个操作。我无法使用此控制器,因为它是一个基本控制器,而不是ApiController

尽管已经有一个控制器正在使用路由WebSiteUrl/api/actions,但是否有方法设置WebSiteUrl/api/token端点

我已经创建了一个apicontroller,它使用以下路径:WebSiteUrl/api/controller/action


谢谢你的帮助

普通MVC控制器实现控制器,如果要创建API控制器,必须创建一个将实现ControllerBase抽象类的控制器

例如,一个API控制器看起来像

[Route("api/[controller]")]
[ApiController]
public class ExampleController : ControllerBase
{
    [HttpGet('getall')]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
    .......
}
因此,如果您运行对“websiteUrl/api/token”的get请求,它将指向

[HttpGet]
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}
[HttpGet]
公共IEnumerable Get()
{
返回新字符串[]{“value1”,“value2”};
}
如果要将url重定向到所需路径,请在项目上转到launchsettings.json,如果使用IISExpress,则在配置文件-->IISExpress集合“launchUrl”内的json文件中:

[HttpGet]
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}