Posts

How to add SSL to Website...

The steps to add the SSL to a website are as follows.
1) We need to request for the CSR (Certificate Signing Request) from where our website is hosted.

What is a CSR?
 A CSR or Certificate Signing request is a block of encoded text that is given to a Certificate Authority when applying for an SSL Certificate. It is usually generated on the server where the certificate will be installed.

2) Once requested you need to provide the below mention details.
Common Name (Domain Name)
Organization Name
Organizational Unit  (Department)
City/Locality
State/County/Region 
Country  
Email address  


Once done they will provide you with the CSR

CSR Format
Example

-----BEGIN CERTIFICATE REQUEST-----
MLLByjCCATMCAQAwgYkxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
Q0uA0aVog3f5iJxCa3Hp5gxbJQ6zV6kJ0TYsuaaOhEko9sdpCoPOnRBm2i/XRD2D
8iNh8f8z0ShGsFqjDgFHyF3o+lUyj+UC6H1QE7bn
-----END CERTIFICATE REQUEST-----
Note*- When you are trying to generate the SSL Certificate copy all the CSR including the -----BEGIN CERTIFICATE …

Restrict modifying the URL or Route in MVC

To restrict the user from modifying the URL. Write the following code in the Global.asax.cs file.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
        public class NoDirectAccessAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (filterContext.HttpContext.Request.UrlReferrer == null ||
                            filterContext.HttpContext.Request.Url.Host != filterContext.HttpContext.Request.UrlReferrer.Host)
                {
                    filterContext.Result = new RedirectToRouteResult(new
                                   RouteValueDictionary(new { controller = "Error", action = "Index", area = "" }));
                }
            }
        }

Then add this Action Filter to Controller [MvcApplication.NoDirectAccess]

How to handel Global Level Exception's

We can handle the Global Level Exceptions by creating the custom exception filter in our application. To create custom exception filter do the following things.
1) Create a Class in the App_Start folder inherit the IExceptionFilter interface.
2) Override the OnException() method.
3)  Create the object of the System.Exception Class.

public class LogFilter : IExceptionFilter
    {
                public void OnException(ExceptionContext context)
        {
                    Exception ex = context.Exception;
            if (!(ex is HttpException)) //ignoring the file not found exception
            {              
             //Do your stuff.....!!!
            }
        }
    }

4) Register the new Custom Filter into the Filter.Config.cs

filters.Add(new LogFilter());
5) Register the filter in Global.asax.cs file.

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
Note*- For using this we must enable the custom exception in the Web.config file and use the [Handle Error] action filter on eit…

Handling Errors in MVC

We can handle the errors in the MVC application by just enabling the custom errors tab in the web.config file.
<customErrors mode="On" defaultRedirect="Error">  This will only handle the Internal Server Error Exceptions like HTTP Status Code 500 and won't handle the HTTP Status Code Exceptions like 404,405 etc for that we need to do the following.
   <customErrors mode="On" defaultRedirect="Error">
      <error statusCode="404" redirect="Error/NotFound" />
    </customErrors> Here Error is the Controller Name and NotFound is the name of the Action Method after that we need to set the [HandleError] action filter on either the Controller or the Action Method as per our requirement.

Error [1001] While Installing Windows Service

If you encountered an error 1001 while installing a Windows Service then the solution for that is.
1) just check if you have UnInstalled the Windows Service.
2)  if after UnInstalling also the error remains then.
3) One more option is that Open Task Manager and under the Service Section tab look for Your Service Name then select the service go to properties => go to details option.
4) kill the process tree and try to install the service again

Creating Setup.exe for Windows Service

To create the Setup.exe for Windows Service using the Install Shield Tool available in the Visual Studio. Please follow the steps below for creating the setup.
1) File=>New => Project => Templates => Other Project Types => Setup & Deployment.
2) Select the Install Shield option.
3) Project Assistant window will open.
4) The first two tabs Application Information & Installation Requirements where you need to fill all the company name, product name related information.
5) In the Application Files tab, this is the main tab where you will add your .exe file. In the left side, you will find the [Program Files Folder] expand it you will see the folder with the Company Name  [INSTALLDIR] created (with the company name which you have given in the application information tab).
6) Select the Company Name [INSTALLDIR]  then click on Add Files.
7) A new pop up will open browse the location of the. .exe  file where you have kept it and copy paste all the files inside it.
8)…

Few things to remember on WebApi

1) Use the Attribute routing which is the best and easy way I guess you can use for routing and the best part is it avoids the confusion. 2) Use the [RoutePrefix("api/v1/new")] attribute on the controller so that you don't need to declare "api/v1/new" inside each and every method you write.
publicclassNewController : ApiController { [Route("api/v1/new/UserDetails")] [HttpGet] publicHttpResponseMessage UserDetails() { return Request.CreateResponse(HttpStatusCode.OK, "Hello People."); } } AFTER [RoutePrefix("api/v1/new")] publicclassNewController : ApiController