简体   繁体   中英

With attribute routing enabled, and no default routing, why is my API still returning on the old default route?

Note: I'm new to C#, asp.net, and all the things that go along with it.

I've created a class called Review.cs with some attributes.

I also have a controller called ReviewController.cs . The idea behind this controller is that I should be able to have three different components:

1) GET all reviews 2) GET all reviews where siteID = a parameter 3) GET one review

Originally, it was working when I would go to http://localhost:#####/api/Review to find all the reviews, and it also worked when I appended a parameter.

Someone on another question I asked recommended I read up on Attribute Routing , which I did at the link.

As a result of my reading, my WebApiConfig.cs file now looks like this:

using System.Web.Http;

namespace Test_Site_1
{
    public class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //attribute routing
            config.MapHttpAttributeRoutes();

        }
    }
}

My ReviewController.cs file looks like this:

namespace Test_Site_1.Controllers
{
    public class ReviewController : ApiController
    {
        private ReviewAPIModel db = new ReviewAPIModel();

        [Route("api/Review/all")]
        [HttpGet]
        public IEnumerable<Review> Review()
        {
            return db.Review;
        }

        [Route("api/Review/{siteID}/Reviews")]
        [HttpGet]
        public IEnumerable<Review> FindReviewBySite(int siteID)
        {
            return db.Review.Where(Review => Review.siteID == siteID);
        }

        [ResponseType(typeof(Review))]
        [Route("api/Review/single/{id}")]
        [HttpGet]
        public IHttpActionResult Review_ByID(int id)
        {
            Review review = db.Review.Find(id);
            if (review == null)
            {
                return NotFound();
            }

            return Ok(review);
        }
    }
}

Now, based off of my understanding of the multitude of SO questions and the numerous Microsoft guides I've gone over, the Attribute Routes that I've set up should work as follows:

http://localhost:#####/api/Review Expected: should no longer return anything (this is the old default that I removed from WebApiConfig.cs ). Actual: returns all reviews

http://localhost:#####/api/Review/all

Expected: should return what used to be returned by the default API above.

Actual: Returns the error The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Http.IHttpActionResult Review_ByID(Int32)'

http://localhost:#####/api/Review/1/Reviews

Expected: should return all reviews for the site with ID = 1.

Actual: Returns the error HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

http://localhost:#####/api/Review/single/1

Expected: should return me the review with ID = 1.

Actual: returns the error HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Where am I going wrong, or what am I missing? It seems like everything should be set up right based off the guides and SO questions I followed.

I found this question: MVC Attribute Routing Not Working

It suggested that I need to update my Global.asax.cs file to the following:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        GlobalConfiguration.Configure(WebApiConfig.Register); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM