Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's so RESTful about ASP.NET MVC?

REST has been such a popular buzzword for the last couple of years (or so) and when ASP.NET MVC rolled out, everyone was relating REST with ASP.NET MVC. I also fell for the buzz and from the lack of my knowledge, my understanding of REST was simply just:

REST = SEO/User friendly URLs

But it's so much more. And the more I learn about REST the less I relate ASP.NET MVC with it. It is of course much closer to REST than WebForms. So the truth is actually quite the opposite:

REST ≠ SEO/User friendly URLs

And having your default route defined as controller/action/id is definitely not RESTful.

Let me explain my problem with this comprehension.

If ASP.NET MVC was RESTful, we wouldn't have default route defined as:

controller/action/id 

but rather

resources/id  /* that would have to use HTTP methods GET/PUT/POST/DELETE */ 

So instead of having (also providing HTTP method with request path):

/product/index/1  /* GET */ /product/create   /* POST */ /product/delete/1 /* POST */ /product/update/1 /* POST */ 

it should be (HTTP method provided here as well)

/products/1  /* GET */ /products    /* POST */ /products/1  /* DELETE */ /products/1  /* PUT */ 

Now that would be RESTful. The good thing is that this is actually possible. And if you'd make it fully RESTful it would also mean that you'd have to use Ajax because PUT and DELETE methods can not be done with browser-only requests (this is not completely true1). So modern Ajax applications can actually be completely RESTful.

Ajax is client technology and doesn't really have anything to do with ASP.NET MVC. Fact is that ASP.NET MVC can be done as a fully RESTful application. The means of achieving it (Ajax) is not important. (thanks to Darin Dimitrov)

The main question

Why do we consider ASP.NET MVC as a RESTful framework especially relating its URL routing to it? Why didn't they define default URL route to enforce RESTfulness? I'm not looking for argumentative answers but those that actually answer the question - how did this relation come into life... Maybe I'm still not wise enough and still take this as the lack of my knowledge about both.

1Updated info

Actually you don't have to use Ajax to implement fully RESTful architecture. Asp.net MVC supports (since version 2) HTTP method overriding, meaning you can issue PUT or DELETE methods using browser forms. All you have to do is add an additional hidden field like:

<input type="hidden" name="X-HTTP-Method-Override" value="DELETE" /> 

Asp.net MVC framework will be able to understand such POST request as a DELETE request and HttpDeleteAttribute action method selector will also understand it as a delete request. HTTP Method overriding FTW!

like image 622
Robert Koritnik Avatar asked Sep 16 '10 20:09

Robert Koritnik


People also ask

What is RESTful service in ASP.Net MVC?

Acknowledgments. A RESTful service is a webof resources that programs can navigate.

Can we use REST API in MVC?

First, create an ASP.NET Web application project in Visual Studio and call it RestAPI. You can do this by selecting File->New->Project->ASP.NET Web Application and clicking OK. After clicking the OK button, the following window would get displayed from where you need to select the Web API and click the OK button.

What is RESTful services in asp net?

What is REST or RESTful service? REST stands for Representational State Transfer. It is an architectural concept for building inter-operable light weight web services which helps to access and manipulate the web resources identified though URI (Uniform Resource Identifier).

Why ASP.Net MVC is better?

The main advantages of ASP.net MVC are: Enables the full control over the rendered HTML. Provides clean separation of concerns(SoC). Enables Test Driven Development (TDD).


2 Answers

There's nothing preventing you from having routes like resource/id with HTTP methods GET/PUT/POST/DELETE in ASP.NET MVC. It's not the default routes setup but you can do it.

EDIT (MLaritz - Adding Darin's comment): ASP.NET MVC is a server side technology allowing you to expose RESTful urls. The way they are consumed doesn't matter. You asked about why ASP.NET MVC is considered RESTFul technology and the answer is because you can easily expose RESTFul urls for consumption, it's as simple as that.

like image 152
Darin Dimitrov Avatar answered Oct 07 '22 17:10

Darin Dimitrov


I think alot of the buzz had more to do with how un-RESTful the .NET web stack was before MVC and how much easier MVC made it to build RESTful apps on the .NET platform than any particularly RESTful capabilities ASP.NET MVC has.

like image 23
Wyatt Barnett Avatar answered Oct 07 '22 15:10

Wyatt Barnett