4 ms·
Recently I decided to use the ASP.Net MVC framework for a project and tried to combine the API into the web site project for ease. It became quick to me all th
by rjd 15y ago
Recently I decided to use the ASP.Net MVC framework for a project and tried to combine the API into the web site project for ease.
It became quick to me all the mentions of REST within the framework where fictitious. What it actually is an object API exposed via HTTP, its not REST at all.
Thinking I mis-understood what REST was I started doing some research only to discover than no I was correct in my understanding (from white papers) and secondly almost ever single developer article I read (blogs) was wrong. There is a fundamental misunderstanding of what REST is out there. Its kind of disheartening to see how many people just don't get it, and are perpetuating falsehoods :/
ASP.net's main problem is technical. (and I assume this is the same for many languages) You can't have functions with the same parameters. You can't have a separate endpoint methods for POST, GET, PUT, DELETE etc... So if you try to shunt data objects around using it you can't just put them back where you got, of do smart discovery, to things i like from REST. So the whole framework falls to pieces, its designed to mimic REST, but not be REST.
For .net devs reading this I'd avoid the MVC framework for REST. But if you have to for what ever reason you will have to build your own "verb" dispatchers, and drop the use of "action" functions in your controller classes, only have one public method, the standard Index() one, expose nothing else. I may float my samples online at some point but I'm to busy at the moment so I hope this is enough to help.
- rodh257 15y agoYou can't have functions with the same parameters, but ASP.NET MVC gives you a way around this using attributes. See this SO question. http://goo.gl/wJ4jE http://goo.gl/wJ4jE Does that solve what you were talking about?
- sk5t 15y agoSeconded - grandparent post is way off with regard to ASP.NET MVC. See all the concrete classes of ActionMethodSelectorAttribute.
- rjd 15y agoMan you're a life saver, I spent an entire afternoon researching this, and finally gave up after dozens of blogs said it was impossible. Infact an article on stack overflow explained the whole situation as impossible so I gave up researching and wrote my own dispatcher.
- darylteo 15y agoYou can also use [HttpGet], [HttpDelete], [HttpPost], [HttpGet] attributes. Personal preference.
- rodh257 15y agoyeah but the problem he is having is that you'd need to have different a different method signature for each of these otherwise it won't compile, and he wants to have the same path/parameters for each, which is why you need to rename your method and then add the different attribute. If your methods already have different parameters or a different name, you can just use the attributes like you state.
- rjd 15y agoOK thanks once again, got it work. Decided to keep using my previous dispatcher to handle webdav verbs, plus I've already written exception handlers and stuff. Great result for me :)
- hammerdr 15y agoYou should look at Restful Routing (it's on GitHub and I'm on my phone, sorry for lack of link). It provides a rails-like method for doing everything you're asking. I am not sure how this drops into Razor since I haven't used it with MVC3 views yet