Extension methods Using C#

Hi Friends,

In today’s section we’ll start discussion on extension methods. This is one of the powerful feature of c#, which helps you to extend the codebase by adding methods in the existing c# built in method. so, what these extension methods will do, basically it is treated as a shorthand or reusable component in my view which you find useful to be used in various places. hence, in that case, what i would be doing i’ll be creating one static method basically extending any .net type and adding my desired method in it. we can also extension methods to Classes,structs and interfaces. However, we can also extend generics like List<T> or IEnumerable<T>. so, basically it’s a enabling technology which is adding a third party codebase. so, Extension methods are basically easy to use and simple to write.

so, let get started. so, for this am going to add a new class library as shown below. Now, am going to arrange my extensions in one folder called my extensions. so, there are couple of rules associated with extension methods, these are there has to be static class and the method also has to be inside the static class and the last one is that type which we are extending should go into the signature with “this” keyword.


so, before we write the complete extension method, lets create test method for the same, so that we can see that how to consume these extension methods.


so, in the above case, i have written a extension method just to reverse the name, and if you have seen the same, this is very powerful technique to create the custom logic. so, let’s suppose we have some requirements which is custom to the project and is getting reused at various places, so for implementing the same, we could easily use Extension Methods. so, with this i would like to wrap this session, in the next session, we’ll delve more in extension methods. Till then stay tuned and happy coding.

Happy Coding

1,560 total views, 1 views today

Posted in C# | Tagged

Working with promises and web api

Hi Friends,

In the last Post we have seen that how to post a form basically to an MVC action using Angular. we have also verified data being posted @ the chrome (client) level and at the server level as well. Now, moving ahead, let’s see how to handle the incoming server responses in this case. So, basically we want to handle 2 scenarios where in it successfully post the data and the other one where in it gets failed to post the data. So, let’s 1st see the happy path scenario.

95th 96th

so, here is the same thing which we have seen in the previous case. we have basically added $q library to handle the promises. Here, what am doing is upon successful submission am redirecting user to the movies page as shown below.



However, currently the movie didn’t get added. So, will handle this case later, but good news is that on successful submission we get redirected to Movies page. Now, let’s handle the scenario, where in returned some error while saving the same. So, to quickly test the same instead of returning 200 ok status from the PostMovie Controller let’s return internal server error.


Now, when i build and refresh the same and then try to post the result, nothing will happen because we have not handled the error response scenario in our case yet. But, we can inspect the same in the chrome console as shown below.



No, problem now let’s go ahead and handle the same as well.

100th 101th


so, what will happen here, when user hits Post Movie, if it is successful then in that case it will redirect it to the movies page, now if there is some error while saving the same then it will just the error flag to true, which we will handle in our html to produce the error on the screen.


so, what will happen here, this ng-show will become active when error flag returned true from the controller. so, the above said message will be published on the screen.


now, let’s see that how to achieve the same using web api. so, here what will do MVC will take care of serving the views and web api will take care of serving the endpoints. so, let’s get started changing the same with movies controller as shown below.


so, with web api we don’t care of returning JSON explicitly, it’s the property of web api to return the json of the underlying model by default, so we just need to pass on the method in there and it will take care. so, now i have to modify my movies repository as well, i need to include api in the url. well this is how web api works.



Now, when i build and refresh then i can see that api is working properly. Now, let’s go ahead and make the same changes to reviews part.

106th 107th

Download Link:- MVCPlusAngular

So, with this i would like to wrap this module and in the next section we’ll see more on angular validation and using resources more effectively. Till then, stay tuned and happy Coding.

Happy Coding

2,127 total views, no views today

Posting Data with an MVC action

Hi Friends,

In today’s discussion we’ll discuss more on posting data to an MVC action. So, for that i need to 1st create an user interface which will allow us to post some data back to the server. so, am going to create a link in the index page which will navigate to Post Movie Page as shown below.



and subsequently i do need to setup the route in the modules page as shown below.


Now, i need to create one PostMovies template as shown below.


so, notice this form is using that ng-model to bind to a movie object. so, this will create a movie object to angular scope and the movie object will have name, director, Releaseyear and the same i’ll be wiring up with angular controller. But, 1st let’s go to the MVC controller and make changes.


Now, again i need to make some minor changes in the html page for the same as shown below



so, as you can see above i have added one button to carry the model, which is basically handled by ng-click event where in it is taking movie model with it. So, now i have to create that PostMovie angular controller and it’s repository for the same.



so, now i just need to add the newly added scripts on the index page.



so, let’s build and refresh the page and see the behavior.


Now, let’s try and post some values in it.


so, now Test the post Movie event in the network tab of chrome debugging tool 1st, whether we are on right page or not.

91th 92nd

so, when you see the network tab you can see that 200 response is getting returned which means that data is getting posted to the server correctly also, when you see the header tab you will see the data being supplied from the UI itself. Now, in order to verify @ the controller side just put one debug point @ PostMovie MVC Controller and try to see the values receiving @ controller’s end.


So, with this i would like wrap this section. With this am attaching solution for your reference as well. What you can do from here is extend the same according to your need . Till then stay tuned and Happy Coding.

Download Link:- MVCPlusAngular


1,842 total views, 1 views today