How to enable attribute routing in ASP .NET MVC?

To enable attribute routing in asp.net mvc we must call a method named “MapMvcAttributeRoutes” in RouteConfig class. The method “MapMvcAttributeRoutes” will be present in the class “RouteCollection” So we can call the method like below ,

routes.MapMvcAttributeRoutes();

Attribute Routing MVC
Attribute Routing MVC

The method will be executed while initializing the routers.
Description of the method from the class is “ Maps the attribute-defined routes for the application.”
Below is the full code of RouteConfig class with attribute routing enabled

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

What is HTTPS ?

HTTPS
HTTPS
  • HTTPS stands for Hypertext Transfer Protocol Secure.
  • It is related to web concepts and data transmissions over the network.
  • A protocol to handle data transfer between browser and server.
  • URL starts with “https://” if https.
  • HTTPS is secured.
  • HTTPS send data over the port 443.
  • There is need of SSL certificated for HTTPS.
  • There is encryption happens before sending the data from the browser.
  • The people tracking the network cannot see the data we are sending.

What is HTTP ?

HTTP
HTTP
  • HTTP stands for Hypertext Transfer Protocol.
  • It is related to Web Concepts and data transmissions over the network.
  • A protocol to handle data transfer between browser and server.
  • URL starts with “http://” if http.
  • HTTP is not secured.
  • HTTP send data over the port 80.
  • There is no need of SSL certificated for HTTP.
  • There is no encryption happens before sending the data from the browser.
  • The people tracking out network can see all the data we are sending.
  • We must use HTTPS instead of HTTP to make secure data transmission.

What is SQL Injection?

SQL Injection
SQL Injection

SQL Injection is a type of injection attack. An attacker can use it to make a web application process and execute injected SQL statements as part of an existing SQL query.

The user will use sql queries in the input area to hack the website.
Consider a website have login page with input user name and password. In the backend code we have created the query dynamically combining the inputs. In this situation, If user try to give sql queries in the input box, there is a chance the query will be executed. If it happened, then user have the control to manage our database.

Types of variables in c#

value and reference type            The c# support two major variable types, they are “Value type and Reference type”. Int, Float, Decimal , Char , etc are the value types and Classes, , Delegates , Interfaces , Arrays , etc are the Reference types.

Now we have seen c# has two different types of variable types. These types must have some difference so lets see how this value and reference type differs.

Value type and Reference type

I hope everyone knows, when ever we initialize a variable there is a memory allocation will happens, we should know this to know about the difference of value and reference types.

Value type

  • Value type variable store data directly into the memory location.
  • If we get any copy of the data to another variable and try to change any variable data , the changed data will affect only to the variable we try to change , other variable remain the old value.
  • Value type data will be saved in Stack memory.

Reference type

  • Reference type variable does not save data directly to the memory location. it stores the reference of the actual data.
  • If we get any copy of the data to another variable and try to change any variable data , the changed data affect every copy of the object.
  • Reference type data will be saved in Heap memory

Example

using System;

class RefClass
{
    public int Value = 0;
}

class TestProgram
{

static void Main() {

    int valtype1 = 0;
    int valtype2 = valtype1;
    valtype2 = 999;
    RefClass refObj1 = new RefClass();
    RefClass refObj2 = refObj1;
    refObj2.Value = 999;
    Console.WriteLine("Value type values: {0}, {1}", valtype1, valtype2);
    Console.WriteLine("Reference type values: {0}, {1}", refObj1.Value, refObj2.Value);
}

}

 

Output

Values: 0, 999
Refs: 999, 999

Value type

check the above example valtype2 is assigned from the variable valtype1 and changing the data valtype2. but in the output the change has been reflected only to the changed variable.

Reference type

check the above example refObj2 is assigned from the object refObj1 and changing the data inside the object refObj2. The change we made was in refObj2 but in the out put the change applied in the refObj2 reflected refObj1.

The requested page cannot be accessed because the related configuration data for the page is invalid in MVC

The requested page cannot be accessed

The error message “The requested page cannot be accessed because the related configuration data for the page is invalid in MVC”  is occurred due to invalid code in the web.config file. Invalid code in the sense you may given a invalid tag like for <connectionString> you may given <constr>. it makes the error we should use the current xml tag names.

Please see the below examples

Actual tag code for connection string

<connectionString>

you may be made mistake like

<connectionstring>

See the above example the ‘S’ letter for the two example is different because the web.config file xml tags are case sensitive. it should follow the visual studio rules to avoid errors.

In web.config xml file we should specify the tags currently according to the visual studio needs.

if anything wrong in the web.config file you will get the error like

The requested page cannot be accessed because the related configuration data for the page is invalid in MVC

 

How to use switch statement in c# ?

Switch statement is used to check a variable with a set of conditions called cases. We can check a variable whether the value satisfies certain conditions.
For example below I have checked a variable is A, B or C. in some case we need to do some operations according to the check then we can go for the switch case.

Syntax for switch statement in c#

switch(expression_data) {
case condition1 :
Method1(s);
break; /* optional */
case condition2 :
Method2(s);
break; /* optional */
.
.
.
.
.

default : /* Default is optional */
methodN(s);
}

Example for switch statement in c#

using System;
namespace FindVariable
{
class Program
{
static void Main(string[] args)
{
char character_variable = 'B';

switch (character_variable)
{
case 'A':
Console.WriteLine("Variable has value A");
break;
case 'B':
Console.WriteLine("Variable has value B");
break;
case 'C':
Console.WriteLine("Variable has value C");
break;

default:
Console.WriteLine("The variable not have the chartectors A,B and C");
break;
}
Console.WriteLine("The variable has value{0}", character_variable);
Console.ReadLine();
}
}
}

Output

Variable has value B
The variable has value B

How to concat 2 strings without using any inbuilt string functions?

String is a character array.Find the length of the first character array, and find the length of the second character array, using a loop concat both strings.

Sample C# code :

string variable1 = “String concat”;
string variable2 = “example.”;
int length1 = variable1.Length;
int length2 = variable2.Length;
char[] concat = new char[length1 + length2];
for (int i = 0; i < length1; i++)
{
concat[i] = variable1[i];
}
for (int i = 0; i < length2; i++)
{
concat[i + length1] = variable2[i];
}

What is the difference between Managed code and Unmanaged code in .Net?

Managed code Vs Unmanaged code

Any code running under CLR(Common Language Runtime)is called Managed code and opposite is called unmanaged code.

Managed Code

When a programmer write code in .Net languages such as C# or VB, he is writing managed code. Because any code which executes under CLR is called managed code. Managed code is subjected to certain rules and has so many benefits. But the rules are so easily for a programmer as the compiler must generate an MSIL(Microsoft Intermediate Language) file which target CLR and use the .Net class libraries. While following certain rules, there are so many benefits like, modern memory management, the ability to mix languages(Use of VB library in C# programming or vice versa), more security support, version controlling and a clean way of software components interaction. This article is about Managed code Vs Unmanaged code

Unmanaged Code

Just opposite to managed code is called unmanaged code. Unmanaged code does not execute under CLR. We cannot write unmanaged code in C# or VB., because the run under CLR. But in windows, prior to the creation of .Net framwork, it uses unmanaged code. For Example, COM components, COM+ Services and Win32 API are examples for unmamaged codes. So it is possible to interact between managed code and unmanaged code in windows.

Explain asp.net page life cycle?

Asp.net page life cycle passes through some series of process during their life cycle. Following are the stages

Begin request

  • This event will show that it’s a new request
  • Object and variables are initialized in this stage
  • This event is raised on every request

Authenticate request

  • .net is ready to authenticate the user in this stage
  • Authentication code are inserted here
  • This request confirms the authentication

Authorize request

  • net is ready to authorize the user in this stage
  • Custom Authorization code can be done here
  • This request confirms the authorization of the user

Resolve request cache

  • Output cache directive is commonly used in asp.net for caching
  • After authorize request is finished, authorize will calls the cache request to serve the request.
  • During runtime asp.net can load data from cache if needed.
  • Any type of cache activity code can be written here

Map request handler

  • Checks the extension of the file
  • Checks the handler of the request

Acquire request state

  • Ready to acquire session state variables/information

Process request

  • Logic execution is done at this stage

Init

  • Control’s id is set at this stage
  • Dynamically add controls at this stage
  • Read control properties

Load

  • Controls are fully loaded at this stage
  • All the values are restored in this stage
  • “Ispostback” is used to avoid unnecessary state change

Render

  • Method of page and page objects
  • Html changes can be mad lastly in this section
  • This method generates client side html and scripts to display

Unload

  • This is to clean up the code like closing connection, closing opened files
  • Page objects are unloaded from the memory

Release request state

  • Save or update session variables are done at this stage

Update request cache

  • Update cache before end of the process

End request

  • Last stage end of the request

 

                               Image shows the page life cycle of an event in GET and POST condition