Question

I'm presented with a problem of using query string variables in a RESTful Asp.net MVC application. Does that violate the RESTful pattern?

The thing is that Asp.net MVC uses default route like:

/controller/action/id

I have to pass through another controller+action combination like:

/controller/action/id/controllerX/actionX

but this won't work if I'd be in the root of my application, because the first three parameters will be omitted and the last two will be treated as the first couple.

Instead of this I'm tempted to use this notation instead:

/controller/action/id?Param=controllerX/actionX

Which would also work on root

http://domain/?Param=controllerX/actionX

But is this RESTful?

A bit of an explanation

I want to have a more modular approach to my views than the normal built-in with PartialViews. Because if I have a partial view that gets displayed on many pages I have to add vide data for that particular partial view in every single controller action that returns a view that uses that particular partial view. And I think that's a wrong way of doing things. Controllers become dependant on partial views that may (or god forbid may not ) be part of the main view.

rendering actions instead of partial views come to the rescue. I use my own Html.RenderAction() that calls into some controller's action that prepares its own data only. Great this works fine.

First problem was with HTTP method that got propagated to all RenderAction actions. So I changed it and I can control HTTP method as I like. Some actions may always be using GET no matter if the main view was posted back (thus using POST).

The problem is when these actions return partial views with their own FORM elements. Since my sub actions can't be directly posted to (because they wouldn't know which view to return) they actually post to the view instead. All sub actions with forms usually have propagated HTTP method from the main view, so they will actually enter postback action execution.

The thing is that only the actual form that was being posted back should execute its POST action. Others should just use GET method. Here's where my second controller/action pair comes into play. Every sub action form posts back to

mainViewController/action/?PostForm=controller/action

where the second parameter helps deciding which sub action rendering should propagate POST method to it. That particular form will be validated as defined in its post action.

I hope this explains my problem a bit more with a concrete example. This problem is related to my yesterday's question.

Was it helpful?

Solution

If this URL will result in the same view returned without dependence on session or cookies or the previous browsing history of a client, then yes, it will be what you call RESTful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top