Pergunta

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.

  1. /api/question/ : list of questions
  2. /api/question/{id} : question of the ID
  3. /api/question/{id}/reply/ : list of replies with the question.
  4. /api/question/{id}/reply/{rid} : one reply item of the question.

Top two urls are not a problem as WebApi CRUD generates for me. But below two urls, I cannot make it.

The methods below are related ones in QuestionController (Note that controller name is 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. does not hit in case 3 and 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.

How can I make it?

Foi útil?

Solução

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
  2. Reply will always expect parameter i.e. QuestionId
  3. If Particualr Reply Id is needed then /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

And for your Method you can try

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

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

Outras dicas

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. A little dirty but if your controller does not have many actions, use it handy.

Otherwise, http://mvccoderouting.codeplex.com/ is a good alternative.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top