简体   繁体   English

子URL的ASP.NET MVC WebApi路由

[英]ASP.NET MVC WebApi Route for sub url

There are many questions in this topic, but I hardly find to address my problem. 这个主题有很多问题,但是我很难解决我的问题。

Likewise Stackoverflow, I would like to get question(master) and replies(details) hierarchical json with below four cases. 同样的Stackoverflow,我想在以下四种情况下获得问题(主)和答复(详细)分层json。

  1. /api/question/ : list of questions / api / question /:问题列表
  2. /api/question/{id} : question of the ID / api / question / {id}:ID问题
  3. /api/question/{id}/reply/ : list of replies with the question. / api / question / {id} / reply /:对问题的答复列表。
  4. /api/question/{id}/reply/{rid} : one reply item of the question. / api / question / {id} / reply / {rid}:问题的一个答复项。

Top two urls are not a problem as WebApi CRUD generates for me. WebApi CRUD为我生成了前两个URL并不是问题。 But below two urls, I cannot make it. 但是在两个网址以下,我无法实现。

The methods below are related ones in QuestionController (Note that controller name is Question). 以下方法与QuestionController中的相关方法有关(请注意,控制器名称为Question)。

// case 1.
public virtual Question Get(){
    // return list of questions
}

// case 2.
public virtual Question Get(Int64 id)
{
    // return question item.
        var item = ((IQuestionRepository)repository).GetFirstFiltered(n => n.Id == id);
        return item;
}

// case 3.
public virtual Question GetReply(Int64 id)
{
    // return a list of replies         
}

// case 4.
public virtual Question GetReply(Int64 id, Int64 rid)
{
    // return reply item of rid.
}

First try 第一次尝试
I added route above default route as below, but only fourth method is triggered. 我在默认路由上方添加了路由,如下所示,但仅触发了第四种方法。 third is omitted. 第三个省略。

            config.Routes.MapHttpRoute(
            name: "QuestionApi",
            routeTemplate: "api/{controller}/{id}/reply/{rid}",
            defaults: new
            {
                controller = "Question",
                rid = RouteParameter.Optional
            }
        );

Second try 第二次尝试
I created newly introduced Route attribute in WebApi2. 我在WebApi2中创建了新引入的Route属性。 does not hit in case 3 and 4. 在第3和第4种情况下不会命中

    // case 3.
    [Route("reply")]
    public virtual Question GetReply(Int64 id)
    {
        // return list of reply
    }

    // case 4.
    [Route("reply/{rid}")]
    public virtual Question Get(Int64 id, Int64 rid)
    {
        // return reply item
    }

Route works if I set from very first address as "~api/{controller}/{id}/reply" , but it doesn't seem to be good solution. 如果我将第一个地址设置为"~api/{controller}/{id}/reply" ,则Route可以工作,但这似乎不是一个好的解决方案。

How can I make it? 我该怎么做?

I wonder why single Controller should have all Methods in it? 我想知道为什么单个控制器中应该包含所有方法?

To make it more manageable its always advised to have multiple controllers. 为了使其更易于管理,始终建议使用多个控制器。

What I can suggest is 我可以建议的是

  1. Make a new controller with name Reply 制作一个名称为Reply的新控制器
  2. Reply will always expect parameter ie QuestionId 回复将始终期望参数,即QuestionId
  3. If Particualr Reply Id is needed then /QuestionId/ReplyId 如果需要特定的答复ID,则/ QuestionId / ReplyId

A route can be 一条路线可以是

config.Routes.MapHttpRoute(
            name: "ReplyApi",
            routeTemplate: "api/{controller}/{id}/{rid}",
            defaults: new
            {
                controller = "Reply",
            }

Update: 更新:

Give a try to http://mvccoderouting.codeplex.com/ It gives more flexibility on Routing 尝试http://mvccoderouting.codeplex.com/,它为路由提供了更大的灵活性

And for your Method you can try 而对于您的方法,您可以尝试

public ActionResult Reply(int Questionid, int? ReplyId) {

   if (ReplyId.HasValue) {
      // ReplyId present
   } else {
      // ReplyId not present
   }
}

To address my problem without help of http://mvccoderouting.codeplex.com/ that @Nipun introduced, I added RoutePrefix to controller class and Route attribute to each method. 为了解决@Nipun引入的http://mvccoderouting.codeplex.com/的帮助而没有解决我的问题,我将RoutePrefix添加到控制器类,并将Route属性添加到每个方法。 A little dirty but if your controller does not have many actions, use it handy. 有点脏,但是如果您的控制器没有很多动作,请方便使用。

Otherwise, http://mvccoderouting.codeplex.com/ is a good alternative. 否则, http://mvccoderouting.codeplex.com/是一个很好的选择。

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

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