Solved

WebAPI POST Route Config?

Posted on 2014-04-05
10
2,164 Views
Last Modified: 2014-04-07
Using VS2012 and WebApi 4

I'm trying to POST a new book title and cannot get the routing correct.

My querystring: http://localhost:51377/api/books/post?name=natgeo

Web page error:

<Error><Message>No HTTP resource was found that matches the request URI 'http://localhost:51377/api/books/post?name=natgeo'.</Message><MessageDetail>No route providing a controller name was found to match request URI 'http://localhost:51377/api/books/post?name=natgeo'</MessageDetail></Error>


[WebApiConfig]
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "post",
                routeTemplate: "api/{Books}/{post}/{name}",
                defaults: new { name = "" }
            );
        }
     }

[Controller]
 public class BooksController : ApiController
 {
        public HttpResponseMessage Post(string name)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (SampleDbEntities entities = new SampleDbEntities())
                    {
                        Book book = new Book
                        {
                            BookName = name
                        };

                        entities.Books.AddObject(book);
                        entities.SaveChanges();
                        return Request.CreateResponse(HttpStatusCode.OK);
                    }
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, "Model state is invalid");
                }
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }
}
0
Comment
Question by:WorknHardr
  • 4
  • 3
  • 3
10 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 39980779
Change your route config to:

routeTemplate: "api/{controller}/{action}/{name}",

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39980906
My querystring: http://localhost:51377/api/books/post?name=natgeo
POST data is not passed in a query string but in another 'container' that is not visible.  The example page you linked to on your other question does show how to do it but it is not as simple as a GET with a query string.
0
 

Author Comment

by:WorknHardr
ID: 39981150
I know JSON is used to POST. I'm trying to learn how to POST or PUT with a URL string.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39981251
@Dave Baldwin

POST data is not passed in a query string but in another 'container' that is not visible.
Technically speaking, there is nothing in the RFC that forbids data from being passed in the querystring of a POST operation. It is generally discouraged, though.

@WorknHardr

My previous comment was off the cuff, so does the fact that you haven't responded to it imply that it did not work?
0
 

Author Comment

by:WorknHardr
ID: 39981393
Sorry Kaufmed, I'm out of the office and haven't applied your code yet!
0
 

Author Comment

by:WorknHardr
ID: 39981403
I'm new to the Web API and trying to learn its capabilities, limitations, pro's, con's, etc...
I'm still curious why it's much easier to GET and so difficult to POST or PUT using a simple query string. It's a SOAP issue I suppose and security concerning illegal WRITES. After becoming more educated about a Web API, I will use AngularJS and JSON to consume a .Net Web API service.
I understand Web API 2 does even more.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39981431
I'm still curious why it's much easier to GET and so difficult to POST or PUT using a simple query string.
Well, as I mentioned above, you can (sometimes) use a querystring with a POST or PUT, but this isn't how most systems operate, so you would be doing something that is non-standard. If you continue down that path, then you would want to ensure that you have appropriate documentation of your non-standard implementation.

Also, you can only do simple key/value with queystring. Complex data should be passed in a request body...which is exactly what is expected when using POST and PUT.

It's a SOAP issue...
Whoa! SOAP and REST are two different things. It is possible to send/recieve SOAP (to an extent, as I understand) via a RESTful service, but I don't think anyone does this. So my question to you would be:  Do you have a SOAP service or a REST service?
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39981446
Sorry but you are misunderstanding the definitions of GET and POST.  Only GET methods use the querystring, POST does not and has not ever used that.  You can use Both methods in a single request but the receiving page has to check both methods when you do that.  On the receiving page, 'request.querystring()' is used to load the GET information and 'request.form()' is used to load the POST information.

All these things are well defined.  http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html is the 'official' but somewhat obscure definition.  http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol is a somewhat more concise description.
0
 

Author Closing Comment

by:WorknHardr
ID: 39984040
Yup, this works for routing:    
        routeTemplate: "api/{controller}/{action}/{name}",

And here is the page return:
         <Error><Message>The requested resource does not support http method 'GET'.</Message></Error>

I'm learning, thx...
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39984098
You're welcome, glad to help.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question