Web Api 2路由无法使用guid作为参数进行解析

时间:2014-07-03 18:46:04

标签: asp.net-mvc asp.net-web-api2

我在下面定义了一个类,并使用以下URL命中

此网址有效: http://rooturl.com/website/api/v1/queuedNotifications

此网址没有 http://rooturl.com/website/api/v1/queuedNotifications/4b2a366b-e349-468d-99e6-b60918dc79f9

此网址没有 http://rooturl.com/website/api/v1/notifications/4b2a366b-e349-468d-99e6-b60918dc79f9

它没有将URL的末尾的GUID识别为GET的参数。谁能告诉我我可能做错了什么?我将约束放在GUID的路由前缀上,参数名称匹配......不确定是什么。

我的网址无法正常使用404.0错误

<fieldset>
<legend>Error Summary</legend>
<h2>HTTP Error 404.0 - Not Found</h2>
<h3>The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.</h3>
</fieldset>

班级定义

[RoutePrefix("api/v1")]
[Authorize]
public class NotificationsService : BaseApiService
{
    private readonly INotificationsService _notificationsService;

    public NotificationsService(INotificationsService notificationsService)
    {
        _notificationsService = notificationsService;
    }

    #region UserNotifications

    [Route("notifications/{userId:guid}")]
    [AllowAnonymous]
    [AcceptVerbs("GET")]
    public async Task<List<UserNotificationModel>> GetNotifications(Guid userId)
    {
        return await _notificationsService.GetNotifications(userId);
    }

    [Route("notification")]
    [AllowAnonymous]
    [AcceptVerbs("POST")]
    public async Task<UserNotificationModel> CreateNotifications(UserNotificationModel model)
    {
        return await _notificationsService.CreateNotification(model);

    }
    #endregion


    #region Queued Notifications
    [Route("queuedNotifications")]
    [AllowAnonymous]
    [AcceptVerbs("GET")]
    public async Task<List<NotificationsQueueModel>> GetQueuedNotifications()
    {
        return await _notificationsService.GetQueuedNotifications();
    }

    [Route("queuedNotifications/{userId:guid}")]
    [AllowAnonymous]
    [AcceptVerbs("GET")]
    public async Task<List<NotificationsQueueModel>> GetQueuedNotifications(Guid userId)
    {
        return await _notificationsService.GetQueuedNotifications(userId);
    }
}

0 个答案:

没有答案