1

I am wondering if it is possible to do following with ASP.NET MVC 5.

I would like to have a OrderController and the following folder's structure

View/Orders/Details/

I need to know how we can configure methods for Details folder?

I mean Create/Edit/List.

Have we use some method attribute for it or routing and how it should be done?

Thanks!

P.S.

I found very useful this link http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • You mean you want to, in your action method, do this `return View("~/Views/Orders/Details/Create.cshtml");`? – DavidG Oct 14 '14 at 12:14
  • So have I answered your question with that comment? – DavidG Oct 14 '14 at 12:15
  • What do you mean by "configure access"? Or "configure methods"? What are you trying to accomplish? Are you asking about authorization for action methods? – David Oct 14 '14 at 12:15
  • @DavidG Yes, please. I will accept it. – NoWar Oct 14 '14 at 12:15
  • @David I mean if I put method `Create` into `OrderController` for `View("~/Views/Orders/Details/Create.cshtml");` have I adjust it somehow so it will be executed? – NoWar Oct 14 '14 at 12:17
  • @ClarkKent: You seem to be confusing some terminology here. Any method in a controller which returns an `ActionResult` can return any view you'd like. What have you tried that isn't working? – David Oct 14 '14 at 12:19
  • @David. I mean I cannot to have two `Create` methods for `Order` and `Details` at the same controller. But I need to. – NoWar Oct 14 '14 at 12:20
  • 1
    @ClarkKent: You can have two methods with the same name as along as they're distinct overloads of one another. If they accept different arguments, they can both be on the same controller. Though *semantically* that's probably not a good idea since controllers usually equate to some semantic concept and having two in the same controller could get confusing. – David Oct 14 '14 at 12:24
  • @David How do you suggest to resolve that? Could you provide some answer, please? – NoWar Oct 14 '14 at 12:34
  • 1
    @ClarkKent: Resolve *what*? How to have two methods with the same name in the same class? The answer below already illustrates that, the method parameters just need to be different from each other. Or do you mean how to put semantic concepts into their own controllers? Just create two separate controllers. In this case you might have an `OrdersController` and an `OrderDetailsController` if the two concepts really are different from one another. It's difficult to be more specific without knowing your modeling and use cases. – David Oct 14 '14 at 12:45

1 Answers1

4

You have two options. You can either make your own code to determine the correct view to return which is fairly complex or you can specify the view you need using the full path. Additionally if you have to have methods with the same (not sure why you would want that), then you would need to change your routing. An option is to use attribute routing.

public class OrdersController : Controller
{
    [Route("CreateOrder")]
    public ActionResult Create(Order order)
    {
        //Snip
        return View("~/Views/Orders/Details/Create.cshtml");
    }

    [Route("CreateOrderDetails")]
    public ActionResult Create(OrderDetails orderDetails)
    {
        //Snip
        return View("~/Views/Orders/Details/Create.cshtml");
    }


}
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • David, I need to know what do I have to do with `Create` method of `OrderController`. Will it react to that path? I mean have I use Routing or something else for that method? And how it should be done. – NoWar Oct 14 '14 at 12:18
  • I see. But, David I have the method with the same name for `Order`. How to resolve it? I mean I am gonna keep at the same controller methods that have the same name but for different Views in folder's chain. – NoWar Oct 14 '14 at 12:21
  • You either rename one (perhaps `OrderDetails`) or use routing which effectively does the same thing for the URL – DavidG Oct 14 '14 at 12:23