简体   繁体   English

以与控制器相同的前缀开头的路由不起作用

[英]Routes that starts with the same prefix as controller not working

Why Routes that starts with the same prefix as controller not working? 为什么以与控制器相同的前缀开头的路由不起作用? With below code on my controller I am making a get call where RoutePrefix is same as the controller name. 在控制器上使用以下代码,我进行了一次get调用,其中RoutePrefix与控制器名称相同。 Now I am making a call as something like http://[localhost]/subscribers which is not working. 现在,我正在拨打类似http:// [localhost] / subscribers之类的电话,但该电话无法正常工作。

namespace WebApi.Controllers
{
 [RoutePrefix("subscribers")]
 public class SubscribersController : ApiController
 {
    [Route("")]
    [HttpGet]
    public IQueryable<string> Get()
    {
        return new string[] { "value1", "value2" }.AsQueryable();
    }
 }
}

I tried something as Route("~/subscribers") instead of just Route("") but that is not working as well. 我尝试了一些类似Route(“〜/ subscribers”)的方法,而不仅仅是Route(“”),但是效果不佳。

The option that is working for me currently is http://[localhost]/subscribers/all with below code 当前对我有用的选项是http:// [localhost] / subscribers / all,其中包含以下代码

namespace WebApi.Controllers
{
 [RoutePrefix("subscribers")]
 public class SubscribersController : ApiController
 {
    [Route("all")]
    [HttpGet]
    public IQueryable<string> Get()
    {
        return new string[] { "value1", "value2" }.AsQueryable();
    }
 }
}

How can I make it work as http://[localhost]/subscribers 我怎样才能使它作为http:// [localhost] / subscribers

Both samples work just fine. 两个样本都很好。 I've created a new web project and added both versions to the project. 我创建了一个新的Web项目,并将两个版本都添加到了该项目中。 Try to create a sample project and check if problem persists. 尝试创建一个示例项目,并检查问题是否仍然存在。

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

相关问题 不同路由的同一控制器中的 AmbiguousActionException - AmbiguousActionException in same controller for different routes SetActualResponseType使用相同方法在同一控制器中使用不同的路由 - SetActualResponseType with same method in the same controller with different routes 将两条不同的路线映射到同一控制器动作 - Map two different routes to the same controller action Web Api控制器路由随机停止工作 - Web Api Controller Routes Randomly Stop Working 同一个控制器中的 RedirectToAction 不起作用? - RedirectToAction in same controller not working? 创建具有相同名称但路径前缀不同的其他控制器 - Creating different controller with same name but different route Prefix 不工作的路由总是回到同一页面 - Routes not working always coming back to the same page 如何为具有不同参数的同一Controller方法设置不同的路由? - How to set different routes for the same Controller method with different parameters? 如何在同一控制器的不同路由中封装通用请求处理 - How to encapsulate common request handling in different routes in same controller 如何在 ASP.NET MVC 5 中设置 2 条路由指向同一个 controller - How to set 2 routes point to the same controller in ASP.NET MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM