WebAPI not found

TechIsaac
TechIsaac used Ask the Experts™
on
I am getting all the WebAPI projects with my limited experience but learning as I go.

I have been given a project in which the want a model with multiple functions
and one(1) controller.  I've setup the models and now want to test each function.
I have set up the controller with the one function and modified App_Start as seen below.
The error messages I'm getting are:
"No HTTP resource was found that matches the request URI
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:62587/api/LobTotals/5'.",
 
"MessageDetail": "No type was found that matches the controller named 'LobTotals'."
api/LobTotals/delete_request/{id}

I've tried multiple variations but powermon cannot find URI



Controller

using lob_totals.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace lob_totals.Controllers
{
    public class LobTotalsController : ApiController
    {
        // Delete LOB
        [Route("api/LobTotals/delete_request/{id}")]
        [HttpDelete]
        public IHttpActionResult LobTotals(int id)
        {
           try
           {
               var delete_request = new LobTotals();
               delete_request.delete_request(id);
               return Ok();

           }
         catch (Exception ex)
             {
               return InternalServerError(ex);
             }


        }
        //
    }
}

Open in new window



Model

namespace lob_totals.Models
{
    public class LobTotals
    {
        public int Order_By { get; set; }
        public int Order_by { get; set; }
        public string Lob { get; set; }
        public string lob_num { get; set; }
        public string Total_Date { get; set; }
        public int Total_Num { get; set; }
        public bool Current_Flag { get; set; }
        public string Lob_num { get; set; }
        public int requestor_id { get; set; }
        public int intReturn = 0;
        public string lblTotal = "";
        public bool Convert_Flag { get; set; }
        public int request_num { get; set; }


        public void delete_request(int requestor_id)
        {

            using (var con = lob_totals.Models.Database.GetConnection())
            {
                // Delete LOB Totals
                using (SqlCommand command = new SqlCommand("lobtotals_delete_lob", con))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    // execute stored procedure
                    con.Open();
                    //SqlDataReader rdr = command.ExecuteReader();
                    command.Parameters.Add(new SqlParameter("@requestor_id", System.Data.SqlDbType.Int)).Value = requestor_id;
                    using (SqlDataReader rdr = command.ExecuteReader())
                    // Locate row to delete
                    //if (rdr.HasRows)
                    {

                        intReturn = 0;
                        con.Close();

                    }

                    return;
                }
            }
        }
        // Get Lobs Total
        public int Load_Gridview2(int lblTotal)
        {
            using (var con = lob_totals.Models.Database.GetConnection())
            {
                // Get LOB Totals
                using (SqlCommand command = new SqlCommand("lobtotals_get_total", con))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    // execute stored procedure
                    con.Open();
                    //SqlDataReader rdr = command.ExecuteReader();
                    command.Parameters.Add(new SqlParameter("@TotalCount", System.Data.SqlDbType.Int)).Value = lblTotal;
                    using (SqlDataReader rdr = command.ExecuteReader())
                        // Locate row to delete
                        if (rdr.HasRows)
                        {

                            intReturn = 0;
                            con.Close();

                        }

                    return lblTotal;
                }
            }
        }
        // Insert Lob
        public string insert_lob(string Lob, int Total_num, string lob_num)
        {
            using (var con = lob_totals.Models.Database.GetConnection())
            {
                // Insert New Lob
                using (SqlCommand command = new SqlCommand("lobtotals_insert_new_lob", con))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    // execute stored procedure
                    con.Open();
                    //SqlDataReader rdr = command.ExecuteReader();
                    command.Parameters.Add(new SqlParameter("@lob", System.Data.SqlDbType.Int)).Value = Lob;
                    command.Parameters.Add(new SqlParameter("@Total_Num", System.Data.SqlDbType.Int)).Value = Total_Num;
                    command.Parameters.Add(new SqlParameter("@lob_num", System.Data.SqlDbType.Int)).Value = lob_num;
                    using (SqlDataReader rdr = command.ExecuteReader())
                    // Locate row to delete
                    // 
                    {

                        intReturn = 0;
                        con.Close();

                    }

                    return intReturn.ToString();
                }

            }

        }
        // Load App gridview
        public void Load_Gridview()
        {

            using (var con = lob_totals.Models.Database.GetConnection())
            {
                // Lob Select Request
                using (SqlCommand command = new SqlCommand("lobtotals_select_request", con))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    // LOB Select Request
                    con.Open();

                    using (SqlDataReader rdr = command.ExecuteReader())
                        
                        if (rdr.HasRows)
                        {

                            intReturn = 0;
                            con.Close();

                        }

                    return;
                }
            }
        }
        // List Lobs
        public List<LobTotals> GetLobs(List<LobTotals> lobIn, int request_num)
        {

            List<LobTotals> lobs = new List<LobTotals>();

            using (var con = lob_totals.Models.Database.GetConnection())
            {
                using (SqlCommand command = new SqlCommand("lobtotals_select_request_detail", con))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    {
                        // execute stored procedure
                        con.Open();
                        command.Parameters.Add(new SqlParameter("@request_num", System.Data.SqlDbType.Int)).Value = request_num;
                        using (SqlDataReader rdr = command.ExecuteReader())
                            // check for rows
                            if (rdr.HasRows)
                                while (rdr.Read())
                                {

                                    LobTotals lob = new LobTotals();

                                    lob.Order_By = Convert.ToInt32(rdr["Order_By"]);
                                    lob.Lob = rdr["Lob"].ToString();
                                    lob.Total_Date = rdr["Total_Date"].ToString();
                                    lob.Total_Num = Convert.ToInt32(rdr["Total_Num"]);
                                    lob.Convert_Flag = Convert.ToBoolean(rdr["Total_Num"]);
                                    lob.Lob_num = rdr["Lob_num"].ToString();

                                    lobIn.Add(lob);

                                }

                        return lobIn;
                    }
                }
            }
        }
        // Update Lob Totals
        public int Total_Update(int Order_By, int Total_Num)
        {
            using (var con = lob_totals.Models.Database.GetConnection())
            {
                // Update LOB Totals
                using (SqlCommand command = new SqlCommand("lobtotals_total_update", con))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    // execute stored procedure
                    con.Open();
                    //SqlDataReader rdr = command.ExecuteReader();
                    command.Parameters.Add(new SqlParameter("@Order_By", System.Data.SqlDbType.Int)).Value = Order_By;
                    command.Parameters.Add(new SqlParameter("@Total_Num", System.Data.SqlDbType.Int)).Value = Total_Num;
                    using (SqlDataReader rdr = command.ExecuteReader())
                    // Locate row to delete
                    //if (rdr.HasRows)
                    {

                        intReturn = 0;
                        con.Close();

                    }

                    return intReturn;
                }
            }
        }
        // Lob Totals Update
        public int Totals_Update(int Order_By, int Total_Num)
        {
            using (var con = lob_totals.Models.Database.GetConnection())
            {
                // Update LOB Totals Update
                using (SqlCommand command = new SqlCommand("lobtotals_update", con))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    // execute stored procedure
                    con.Open();
                    //SqlDataReader rdr = command.ExecuteReader();
                    command.Parameters.Add(new SqlParameter("@Order_By", System.Data.SqlDbType.Int)).Value = Order_By;
                    command.Parameters.Add(new SqlParameter("@Lob", System.Data.SqlDbType.Int)).Value = Lob;
                    command.Parameters.Add(new SqlParameter("@Lob_num", System.Data.SqlDbType.Int)).Value = Lob_num;
                    using (SqlDataReader rdr = command.ExecuteReader())
                    // Locate row to delete
                    //if (rdr.HasRows)
                    {

                        intReturn = 0;
                        con.Close();

                    }

                    return intReturn;
                }

            }
        }
    }
}

Open in new window



app_Start

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace lob_totals
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApiWithActionId",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}   

Open in new window



Can someone straighten me out?

Thank you in advance,
Isaac
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Isaac,

Please spend 30-40 minutes to understand "How"  Web API really works. Do not follow the code as these videos are 6 years old, just the logic part of doing things in true HTTP way. After that, fire any queries you have and I will be more than happy to clarify it.

If you are already aware of how Web API works, just look at the route you have defined in your controller
 [Route("api/LobTotals/delete_request/{id}")]

Open in new window


PS: Please use CODE tags so that your code becomes easily readable.

Regards,
Chinmay.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Super Thanks Bill :)
Fernando SotoRetired
Distinguished Expert 2017

Commented:
@Chinmay in your first post you state, "these videos", do you have links to them?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Yikes. I somehow missed posting this link: https://docs.microsoft.com/en-us/aspnet/web-api/videos/getting-started/

Thanks Fernando 🙂
Fernando SotoRetired
Distinguished Expert 2017

Commented:
@Chinmay. 👍
TechIsaacIT Contracting

Author

Commented:
Thanks for your help.

Much appreciated.

Isaac

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial