c# - 如果使用命名空间版本路由,如何在 ASP.Net Core API 中设置 RESTful 属性路由

标签 c# rest asp.net-web-api asp.net-core attributerouting

我正在构建 ASP.Net Core 1.1.0 Web API。

作为支持评论,我选择使用 Peter Lazzarino 的版本路由 NuGet 包,以允许我根据版本将 Controller 分离到不同的命名空间(和文件夹)中。我按照 Peter 的建议在 Startup.cs 中进行了配置,并且构建时没有错误。我正在使用

var apiPrefix = "api";  

按照 Peter 的建议,在 Startup.cs 中允许我的所有路由以 api 开头。

我想遵循 RESTful 准则并在我的方法中使用 HTTP 动词属性。

我希望我的客户能够访问 http://companyname.com/api/v1/mycontroller/status由于我的 API 项目中的“Controllers\v1\MyController.cs”文件夹中包含 MyController,因此版本路由库应该能够找到它。

在我的 Controller 中,我有以下内容;

namespace MyProject.Controllers.v1
{
    public class MyController : Controller
    {

        // GET: ~/api/v1/mycontroller/status
        [HttpGet("/status")]
        public JsonResult Status()
        {
            return Json(new { status = "API is running" });
        }

    }
}

但是,导航至 http://companyname.com/api/v1/mycontroller/status给我一个 404 错误。

我曾尝试将此问题作为评论留在 SO 帖子 How can versioning be done in ASP.NET Core Web Api 中,但我在 StackOverflow 中还没有足够高的声誉来发表评论。 :(

所以,希望有人能帮助我。

预先感谢您提供的任何帮助。

最佳答案

您可以按照 the asp.net core documentation 中指定的方式使用 Controller 上的路由属性这样你就可以对你的 api 进行版本控制,但随后你必须将其添加到每个 Controller 以保持你的代码同步,我不知道你可以在全局添加它的确切位置。希望这有帮助

关于c# - 如果使用命名空间版本路由,如何在 ASP.Net Core API 中设置 RESTful 属性路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41368781/

相关文章:

c# - 在 powershell 类中使用 dotnet 接口(interface)

c# - 从 ASP.NET Web API 中的查询字符串绑定(bind)到动态对象

java - 通过 Java Web 服务发送大数据

angularjs - 单页应用程序应该如何提供永久链接?

java - 具有 RESTful 内容提供商的动态网页

c# - NSwag 自定义 JSON 输出

asp.net-mvc - 如何为 ASP.NET 4.5 Web API 创建 MultipartFormFormatter

c# - Array.Copy vs Skip and Take in c#

c# - 在单个字符串中创建带换行符的 SQL 查询

c# - 从 C# 中的 OleDbDataReader 获取检索记录数?