Showing posts with label visual studio. Show all posts
Showing posts with label visual studio. Show all posts

Thursday, November 3, 2016

Create WebAPI application with an empty template


Creating a WebAPI application in Visual Studio using the templates available makes it bloated with tons of nuget package references and other stuff. Following are the steps to make a WebAPI application in Visual Studio with an empty website template.


1) Create an empty Web Application with nothing in it. (no Global asax, no entry in web.config, no JS)


2) Add Microsoft.AspNet.WebApi.WebHost nuget package to the project. This will add the other dependent packages like WebApi.Core etc.

3) Add a Global.asax file and insert these lines
        protected void Application_Start(object sender, EventArgs e)
        {            
            WebApiConfig.Register(GlobalConfiguration.Configuration);
        }

4) Add a App_Start folder with WebApiConfig.cs
 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

5) Add folder - "Controllers"

6) Add HomeController with the following code - 

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

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

    }

Saturday, August 8, 2015

Create a MVC site in Visual Studio with an empty website template (and not using MVC template)

Creating a MVC website in Visual Studio using the templates available makes it bloated with tons of JS, nuget package references and other stuff. Following are the steps to make a MVC  site in Visual Studio with an empty website template.


1) Create an empty Web Application with nothing in it. (no Global asax, no entry in web.config, no JS)
2) Add Microsoft.AspNet.Mvc nuget package to the project.
3) Add a Global.asax file and insert these lines
        protected void Application_Start(object sender, EventArgs e)
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new RazorViewEngine());
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
4) Add a App_Start folder with RouteConfig.cs
public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
5) Add folders - "Controllers","Views","Views\Home".
6) Add a file - "HomeController.cs" under "Controllers" folder with the following content -
 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
7) Add a file - "Index.cshtml" under "Views\Home" folder with the following content -
<h2>Home Index View</h2>
8) Add a web.config under "Views" folder and TAKE CARE OF THE VERSIONS -

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

    <!--
        Enabling request validation in view pages would cause validation to occur
        after the input has already been processed by the controller. By default
        MVC performs request validation before a controller processes the input.
        To change this behavior apply the ValidateInputAttribute to a
        controller or action.
    -->
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>




9) Open the root of the website.

Monday, November 7, 2011

Debugging tips in Visual Studio

1)      Break on all Exceptions (even if it is handled) Go to Debug  > Exception (Ctrl Alt E). This will be useful when you are given an unknown application that collects all the errors and shows it at once on the top of the screen. You do not know the source of the error (or the exception). Just enable this option and find out directly.
2)       A faster way of debugging an error is to do it backwards i.e. go the known bottommost function and start doing a step out (Shift – F11) to climb up and see where it fails. This is better (faster) than debugging from start (top) and doing Next >> Next. The “starting from bottommost” technique will give all the call flows at once in the CallStack window.
3)      If you want to change value of any variable while debugging (during runtime), use immediate window and just write VAR1=”abc” in the window.
Or hover over the variable , write the value there itself and press Enter.
4)       Conditional BreakPoint in Loops: You can attach a breakpoint in a FOR or FOREACH loop and when it hits it, right-click on the breakpoint and you can put a CONDITION say when p.FirstName=="Paul". So you dont have to iterate through the  whole collection one by one. There are other options as well when you right click.
5)      To attach a breakpoint at every place for a particular function (called Function Breakpoint), Go to the breakpoints window à New à Break at function (or press Ctrl + B). Type the function name.