locked
Check the controller class RRS feed

  • Question

  • User-1418567014 posted

    I am not confident my controller class, would you please help me to take a look? Many thanks.

    namespace BandwidthRestriction.Controllers
    {
        [Route("api/[controller]")]
        public class BandwidthController : Controller
        {
            private SettingDbContext _context;
            private readonly ISettingRespository _settingRespository;
    
            public BandwidthController(ISettingRespository settingRespository)
            {
                _settingRespository = settingRespository;
            }
            public BandwidthController(SettingDbContext context)
            {
                _context = context;
            }
    
            // GET: api/Bandwidth
            [HttpGet]
            public IEnumerable<Setting> GetSettings()
            {
                return _settingRespository.GetAllSettings();
            }
    
            // GET: api/Bandwidth/GetTotalBandwidth/163
            [HttpGet("{facilityId}", Name = "GetTotalBandwidth")]
            public IActionResult GetTotalBandwidth([FromRoute] int facilityId)
            {
                if (!ModelState.IsValid)
                {
                    return HttpBadRequest(ModelState);
                }
    
                Setting setting = _settingRespository.GetSettingByFacilityId(facilityId);
    
                if (setting == null)
                {
                    return HttpNotFound();
                }
    
                return Ok(setting.TotalBandwidth);
            }
    
            // GET: api/Bandwidth/GetAvailableBandwidth/163
            [HttpGet("{facilityId}", Name = "GetAvailableBandwidth")]
            public IActionResult GetAvailableBandwidth([FromRoute] int facilityId)
            {
                if (!ModelState.IsValid)
                {
                    return HttpBadRequest(ModelState);
                }
    
                Setting setting = _settingRespository.GetSettingByFacilityId(facilityId);
    
                if (setting == null)
                {
                    return HttpNotFound();
                }
                var availableBandwidth = setting.TotalBandwidth - setting.BandwidthUsage;
                return Ok(availableBandwidth);
            }
    
            // PUT: api/Bandwidth/UpdateBandwidthChangeHangup/163/10
            [HttpPut]
            public void UpdateBandwidthChangeHangup([FromRoute] int facilityId, [FromRoute]int bandwidth)
            {
                _settingRespository.UpdateBandwidthHangup(facilityId, bandwidth);
            }
    
            // PUT: api/Bandwidth/UpdateBandwidthChangeOffhook/163/10
            [HttpPut]
            public void UpdateBandwidthChangeOffhook([FromRoute] int facilityId, [FromRoute] int bandwidth)
            {
                _settingRespository.UpdateBandwidthOffhook(facilityId, bandwidth);
            }
    
            // POST: api/Bandwidth/PostSetting/163/20
            [HttpPost]
            public bool PostSetting([FromRoute] int facilityId, [FromRoute]int bandwidth)
            {
                if (!ModelState.IsValid)
                {
                    return false;
                }
    
                Setting setting = _settingRespository.GetSettingByFacilityId(facilityId);
                if (setting == null)
                {
                    return false;
                }
    
                var totalBandwidth = setting.TotalBandwidth;
                var bandwidthUsage = setting.BandwidthUsage;
                if (totalBandwidth - bandwidthUsage >= bandwidth)
                    return true;
                return false;
            }
        }
    }

    The map route is

     app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller}/{action}/{facilityId?}/{bandwidth?}");
                });

    Tuesday, May 17, 2016 5:33 PM

All replies

  • User2036436350 posted

    Hi ardmore,

    Could you kindly note the problem you are facing please. That would help us speed up towards an accurate answer.

    Regards,

    Praneeth

    Tuesday, May 17, 2016 6:05 PM
  • User-1418567014 posted

    I haven't debugged it. But it certainly has some issues. I want to use Swagger to display the api, it failed(500 error). Please see http://stackoverflow.com/questions/35788911/500-error-when-setting-up-swagger-in-asp-net-core-mvc-6-app.

    So perhaps something is wrong in the route.

    Tuesday, May 17, 2016 6:10 PM
  • User36583972 posted

    Hi ardmore,

    From your description, I found you are using Swagger in your application.

    Your route attributes are wrong. You can change your route like the below (removed the [FromRoute] attributes in your methods).

            // GET: api/Bandwidth/GetTotalBandwidth/163
            [HttpGet("GetTotalBandwidth/{facilityId}", Name = "GetTotalBandwidth")]
            public IActionResult GetTotalBandwidth( int facilityId)
            {           
                //----------------------------
                return Ok(setting.TotalBandwidth);
            }
    
           // PUT: api/Bandwidth/UpdateBandwidthChangeHangup/163/10
           [HttpPut("UpdateBandwidthChangeHangup/{facilityId}/{bandwidth}")]
           public void UpdateBandwidthChangeHangup(int facilityId, int bandwidth)
           {
               _settingRespository.UpdateBandwidthHangup(facilityId, bandwidth);
           }

    The following links for your reference.

    1: ASP.NET Core 1.0 MVC API documentation using Swashbuckle Swagger:

    https://damienbod.com/2015/12/13/asp-net-5-mvc-6-api-documentation-using-swagger/

    2: How To Use Swagger at ASP.NET MVC:

    http://www.dakehe.info/blog/post/how-to-use-swagger-at-aspnet-mvc

    You can also visit the following links for getting more information about Swagger.

    http://swagger.io/community/

    Best Regards,

    Yohann Lu

    Wednesday, May 18, 2016 3:29 AM
  • User-1418567014 posted

    I used the new code and run the service on a docker controller. Use the url of the service. Then I got another error.

    [Route("api/[controller]")]
    public class BandwidthController : Controller
    {
        private readonly ISettingRespository _settingRespository;
    
        public BandwidthController(ISettingRespository settingRespository)
        {
            _settingRespository = settingRespository;
        }
    
        // GET: api/Bandwidth
        [HttpGet]
        public IEnumerable<Setting> GetSettings()
        {
            return _settingRespository.GetAllSettings();
        }
    
        // GET: api/Bandwidth/GetTotalBandwidth/163
        [HttpGet("GetTotalBandwidth/{facilityId}")]
        public IActionResult GetTotalBandwidth(int facilityId)
        {
            if (!ModelState.IsValid)
            {
                return HttpBadRequest(ModelState);
            }
    
            Setting setting = _settingRespository.GetSettingByFacilityId(facilityId);
    
            if (setting == null)
            {
                return HttpNotFound();
            }
    
            return Ok(setting.TotalBandwidth);
        }
    
        // GET: api/Bandwidth/GetAvailableBandwidth/163
        [HttpGet("GetAvailableBandwidth/{facilityId}")]
        public IActionResult GetAvailableBandwidth(int facilityId)
        {
            if (!ModelState.IsValid)
            {
                return HttpBadRequest(ModelState);
            }
    
            Setting setting = _settingRespository.GetSettingByFacilityId(facilityId);
    
            if (setting == null)
            {
                return HttpNotFound();
            }
            var availableBandwidth = setting.TotalBandwidth - setting.BandwidthUsage;
            return Ok(availableBandwidth);
        }
    
        // PUT: api/Bandwidth/UpdateBandwidthChangeHangup/163/10
        [HttpPut("UpdateBandwidthChangeHangup/{facilityId}/{bandwidth}")]
        public void UpdateBandwidthChangeHangup(int facilityId, int bandwidth)
        {
            _settingRespository.UpdateBandwidthHangup(facilityId, bandwidth);
        }
    
        // PUT: api/Bandwidth/UpdateBandwidthChangeOffhook/163/10
        [HttpPut("UpdateBandwidthChangeOffhook/{facilityId}/{bandwidth}")]
        public void UpdateBandwidthChangeOffhook(int facilityId, int bandwidth)
        {
            _settingRespository.UpdateBandwidthOffhook(facilityId, bandwidth);
        }
    
        // POST: api/Bandwidth/PostSetting/163/20
        [HttpPost("PostSetting/{facilityId}/{bandwidth}")]
        public bool PostSetting(int facilityId, int bandwidth)
        {
            if (!ModelState.IsValid)
            {
                return false;
            }
    
            Setting setting = _settingRespository.GetSettingByFacilityId(facilityId);
            if (setting == null)
            {
                return false;
            }
    
            var totalBandwidth = setting.TotalBandwidth;
            var bandwidthUsage = setting.BandwidthUsage;
            if (totalBandwidth - bandwidthUsage >= bandwidth)
                return true;
            return false;
        }
    }

    Click the ERROR in the below image.

    {"schemaValidationMessages":[{"level":"error","message":"Can't read from filehttp://172.28.128.4:5005/swagger/v1/swagger.json"}]}

    Thursday, May 19, 2016 11:58 PM
  • User36583972 posted

    Hi ardmore,

    I suggest you read the following tutorials and adjust your program.

    1: WebApi documentation done right with Swagger:

    https://cmatskas.com/webapi-documentation-done-right-with-swagger/

    2: RESTful Web API Help Documentation using Swagger UI and Swashbuckle:

    http://www.codeproject.com/Articles/1078249/RESTful-Web-API-Help-Documentation-using-Swagger-U

    Best Regards,

    Yohann Lu

    Friday, May 20, 2016 1:51 AM
  • User-1418567014 posted

    Perhaps it is another issue. I can expand the node; I just didn't do it. If I typed the url http://172.28.128.4:5005/swagger/v1/swagger.json directly, I can get the json file. The error I mentioned is something else when read the json file from swagger.io site. 

    If I expand the node and feed a parameter, I get 500 error. For example, I typed 

    http://172.28.128.4:5005/api/Bandwidth/GetTotalBandwidth/163

    Then 500 error, so I think it is still my controller code issue. Thanks for help.

    Friday, May 20, 2016 2:14 AM
  • User36583972 posted

    Hi ardmore,

    From your error message, 500 is an Internal Server Error (thrown when something goes catastrophically wrong). So, I suggest you should recheck your entire program.

    First, you should debug your application and find which code cause the error. Solve problems based on erroneous information.

    Secondly, Check whether the routing rules correct.

    http://www.asp.net/mvc/overview/controllers-and-routing

    If you have any other questions, you can visit the MVC forum for getting better support.

    http://forums.asp.net/1146.aspx/1?MVC

    Best Regards,

    Yohann Lu

    Friday, May 27, 2016 8:37 AM