Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, May 11, 2017

Interesting things about Web API in Asp.net MVC

Few things I learn in WEB API which is little interesting. I have spent lots of time to fix small issues and after doing google I found the solutions for those small issues, so I decided to document those solutions somewhere. The Blog is perfect place to document it so that I can refer it any time.

Here is the list of issues and their solutions which I faced last night.

1) Not able to call WEB API from client application.
                 When I started learning WEB API in ASP.NET MVC, I found most of the examples where the API has been called from the same project and it was working fine, but in production environment we have to call API from client applications. When I tried the same it was not working. 
Solution:  
                You need to enable CORS in order to allow remote call to your web API. 
  • Add System.Web.Http.Cors reference in your project. If you can't find it, add the nuget package for the same. 
  • Go to the  App_Start folder and click WebApiConfig.cs
  • Add these two lines in Register function
    • var cors = new EnableCorsAttribute("*","*","*");
                  config.EnableCors(cors);  // add this line of code to allow cross domain request
       
 
2) Custom API name was not working
                 If you have taken the default template for Web API development, it won't allow you to use your favorite name for the API for example if you want to use SubmitOrder for post API it won't identify from client side call. 
 Solution:
            You need to add Route attribute to tell MVC that this is my API here is the example
            [Route("api/Test/SubmitOrder")]
            public HttpResponseMessage SubmitOrder(FormDataCollection data)
           {
                     // your code goes here
           }

           Before you add Route attribute to your function, you need to add following line in your WebApiConfig.cs file 
            config.MapHttpAttributeRoutes();
 
3) How to return data / object in HttpResponseMessage
               It is always a best practice to return HttpResponseMessage from your web api but how to return the data in the response? Most of the examples either returns data / list of data or string or integer or HttpResponseMessage either. 
 
Solution:
 
              You can include an object in your response like this.
               
        [Route("api/Test/SubmitOrderThreePara")]
        [HttpPost]
        public HttpResponseMessage SubmitOrderThreePara(FormDataCollection frm)
        {           
            Customer cm = new Customer();
            cm.City = "Ahmedabad";
            cm.ContactName = "Test User";
            cm.Address = "Test Address";
           
            return Request.CreateResponse(HttpStatusCode.OK, cm);

        } 



 
 
  
 
 
 


           
 

Wednesday, February 23, 2011

List of Simple Quality Checks while developing a software or web site

15 Basic QC points to check while developing the software / web application.

In today’s world software development is very rapid process. There are very tight deadlines which need to meet by developers. Usually developers finish the development but after it delivered to QC team developer get a big list of bugs find by qc team. And then developer needs to solve bug list overnight which is very tedious job. Herewith I tried to list out common qc points which developer can check at the time of development which make his/her life easier. Hope it will helpful to developers.

  1. Proper tab index of every field in all the pages.
  2. There should be * near mandatory field. And a note in bottom of page which mention that * fields are mandatory / compulsory.
  3. Set the max length property of every input field in a page so that user can’t enter longer text in field.
  4. There should be at least one default button (i.e. Save / Submit etc.)
  5. In windows application every entry form should be close when user press escape key.
  6. There should be product icon on top left corner or every page in case of windows application.
  7. Numeric fields should not accept alpha characters.
  8. Use proper validation for email addresses, phone numbers etc.
  9. During development please check the junk character entry like ', @, #, $ etc. it should be pass on insert, update, record fetching etc.
  10. In case of listing – details page scenario listing page should have features like sorting, paging, search by all required columns, go to page etc.
  11. Every master entry should be entered during transaction form (i.e. if your transaction form has combo box which contain master data then there should be a + button near combo box, user should be able to press this button and that button should open particular master page after inserting new master entry it should be selected in given combo box.)
  12. Use of try .. catch block. Developer should use try catch block on every event handler where user can interact. Also user should not get unhandled exception, there should be proper message to user in case of any exception and that error should be logged in some text file on user’s machine / server in case of web based application.
  13. Use proper validation for avoiding duplicate master entries.
  14. Referential integrity should be maintain, means when user tries to delete any master entry system should check for the existence of referential data in transactions and should give proper message to user. Then take action like deactivate the master or remove all referential transactions whatever best suits as per the requirements of software.
  15. Every field should have a tooltip which can help user to understand the role of that field in the page.