Monday, May 15, 2017

A better way to configure Intelli-sense for Javascript in VS2015

Now, I'm moving toward the direction of developing fat client using Javascript (JS) and I need a
better way of enable the intelli-sense support for Javascript in the fat client.

Earlier, I found out that we can include the reference in each JS file by adding the following line.
When the number of JS files that you are developing are increasing, added this reference line might be headache.

   /// <reference path="jquery-1.7.js" />

Here is a better way:

1. Add a "_references.js" file into "js" folder (let says you are keeping all your JS files in a folder call "js" instead of "scripts").

2. In the Quick Launch bar (located at top right corner), key in "Javascript" and look for "IntelliSense -> References"  option. Clicks on it.



3. Choose "Implicit (Web)" option.
4. In the "Add a reference to current group" field, key in "~/js/_references.js".
5. Click Add button.
6. Click OK.



7. Back to the Solution Explorer. Right click on _references.js and you will find two new options.

- Auto-sync- means that VS will add all the JS files automatically.
- Update Javascript References - this is manually sync the JS files.















Monday, May 1, 2017

Asp.net Core/MVC routing

Routing is necessary for controlling the accessible path in the URL.

1) In the Startup.Configure proc, you can add a route to "ContactUs" that looks like this:

            app.UseMvc(route =>
            {
                // adding a new route for http://localhost:57477/contactUs
                route.MapRoute(name: "contact_us",
                            template: "contactUs",
                            defaults: new { controller = "Home", action = "ContactUs" });

                // if the path in the url is blank, call HomeController.
                route.MapRoute(name: "default",
                                template: "{controller=Home}/{action=Index}/{id?}");

            });

And the HomeController looks like below. Simple and straight forward.

    public class HomeController : Controller
    {
        // returns the default page.
        public IActionResult Index()
        {
            return View();
        }

        //returns the contact us page.
        public IActionResult ContactUs()
        {
            return View();
        }
    }

2) You may also add a new route by adding HttpGet attribute to the method and add Route attribute to the class.

    [Route("[controller]")]
    public class ValuesController : Controller
    {
        // GET values
        // http://localhost:57477/values/
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET values/5
        // http://localhost:57477/values/7890
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value=" + id.ToString();
        }

        // GET values/getjson/5
        //http://localhost:57477/values/getjson/1234
        // returns the data in json format.
        [HttpGet("GetJson/{id?}")]
        public IActionResult GetJson(int id)
        {
            var v = new { v = "value=" + id.ToString() };
            return new ObjectResult(v);
        }
    }