mvc 5 web api routing

 
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes(); 
    }
}
//using


    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        [Route("customers/orders")]
        [HttpGet]
        public string FindOrdersByCustomer() 
        {
            return "Supervalue";
        }

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }


http://www.codeproject.com/Articles/774807/Attribute-Routing-in-ASP-NET-MVC-WebAPI
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

Simple AngularJS Application ( ASP.NET MVC + VS 2013 )

1 Create ASP.NET MVC Application

2 Create WebAPI
    public class MoviesController : ApiController
    {
        public IEnumerable Get()
        {
            return new List {
                new Movie {Id=1, Title="Star Wars", Director="Lucas"},
                new Movie {Id=2, Title="King Kong", Director="Jackson"},
                new Movie {Id=3, Title="Memento", Director="Nolan"}
            };
        }

        public string Get(int id)
        {
            return "value";
        }

        public void Post([FromBody]string value)
        {
        }

        public void Put(int id, [FromBody]string value)
        {
        }


        public void Delete(int id)
        {
        }
    }

3 Change Index.cshtml
@{
    ViewBag.Title = "Home Page";
}

@section scripts {


    }

Title Director
{{movie.Title}} {{movie.Director}}

4 Check result



wcf multithreading

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall, 
                 ConcurrencyMode=ConcurrencyMode.Single)] 

SignalR ASP NET Example

1 install nuget package


2 Add class Add | New Item | Visual C# | Web | SignalR | SignalR Hub Class (v2) 

3 Add Action to HomeController
4 Add view

5 Add Startup class
using Owin;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

6 Test




OData + C#

1 Odata wcf Service

Create
https://msdn.microsoft.com/en-us/library/dd728275(v=vs.110).aspx

Get from browser
https://msdn.microsoft.com/en-us/library/dd728279(v=vs.110).aspx

Use
https://msdn.microsoft.com/en-US/library/dd728278(v=vs.110).aspx

2 OData WebAPI

Create
http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/creating-an-odata-endpoint

Use
http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/calling-an-odata-service-from-a-net-client

simple example reddis c#

1 Download MSI from here
https://github.com/MSOpenTech/redis/releases



2 Install nuget package in your project

3 How to use

using StackExchange.Redis;
//...
            using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("10.10.0.100:6379"))
            {
                IDatabase cache = redis.GetDatabase();

                cache.StringSet("key1", "value");
                cache.StringSet("key2", 25);

                string key1 = cache.StringGet("key1");
                int key2 = (int)cache.StringGet("key2");
            }

async await c# simple example

    public class MegaClass
    {
        public async void StartAsync()
        {
            await Task.Run(() => { LongProcedure(); });
            Console.WriteLine("End");
        }

        public void Start()
        {
            LongProcedure();
            Console.WriteLine("End");
        }

        private void LongProcedure()
        {
            Thread.Sleep(3000);
        }

        public async Task MyMethodAsync()
        {
            int x = await LongRunningOperationAsync();
            Console.WriteLine(x);
        }


        public async Task LongRunningOperationAsync() 
        {
            await Task.Delay(3000); 
            return 1;
        }

        public async Task LongRunningOperationAsync(int _param)
        {
            await Task.Delay(3000);
            return _param;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            var _c = new MegaClass();

            //1 Sync
            //_c.Start();

            //2 Async
            //c.StartAsync();

            //3 Start and return task
            //Task t = _c.MyMethodAsync();
            //t.Wait();

            //4
            //Task task = new Task(_c.StartAsync);
            //task.Start();

            //5 GetResult 
            //Task x = _c.LongRunningOperationAsync();
            //x.Wait();
            //Console.WriteLine(x.Result);

            //6 with param
            //Task x = _c.LongRunningOperationAsync(3);
            //x.Wait();
            //Console.WriteLine(x.Result);

            Console.WriteLine("Wait");
            Console.ReadLine();
        }
    }

c# Simple socket example


client
    public class SynchronousSocketClient
    {

        public static void StartClient()
        {
            byte[] bytes = new byte[4096];

            try
            {
                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

                Socket sender = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);

                try
                {
                    sender.Connect(remoteEP);

                    Console.WriteLine("Socket connected to {0}",
                        sender.RemoteEndPoint.ToString());

                    byte[] msg = Encoding.UTF8.GetBytes("TEST MSG!EOF");

                    int bytesSent = sender.Send(msg);

                    int bytesRec = sender.Receive(bytes);
                    Console.WriteLine("Echoed test = {0}",
                        Encoding.UTF8.GetString(bytes, 0, bytesRec)
                        );

                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();

                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException : {0}", se.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }


    }
server
    public class SynchronousSocketListener
    {

        public static string data = null;

        public static void StartListening()
        {
            byte[] bytes = new Byte[4096];

            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

            Socket listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    Socket handler = listener.Accept();
                    data = null;

                    while (true)
                    {
                        bytes = new byte[1024];
                        int bytesRec = handler.Receive(bytes);
                        data += Encoding.UTF8.GetString(bytes, 0, bytesRec);
                        if (data.IndexOf("!EOF") > -1)
                        {
                            break;
                        }
                    }


                    Console.WriteLine("Text received : {0}", data);

                    byte[] msg = Encoding.UTF8.GetBytes(data);

                    handler.Send(msg);
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("\nPress ENTER to continue...");
            Console.Read();

        }

    }
test console
    class Program
    {
        static void Main(string[] args)
        {

            Task.Factory.StartNew(() => SynchronousSocketListener.StartListening());
            Thread.Sleep(2000);
            Task.Factory.StartNew(() => SynchronousSocketClient.StartClient());
            Console.Read();
            Task.Factory.StartNew(() => SynchronousSocketClient.StartClient());
            Console.Read();
            Task.Factory.StartNew(() => SynchronousSocketClient.StartClient());
            Console.Read();
        }
    }