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.

The anti-forgery token could not be decrypted error in ASP .Net MVC

The anti-forgery token could not be decrypted. If this application is hosted by a Web Farm or cluster, ensure that all machines are running the same version of ASP.NET Web Pages and that the <machineKey> configuration specifies explicit encryption and validation keys. AutoGenerate cannot be used in a cluster.

 

The above error will happen when we have multiple AntiForgeryToken in same form and we try to submit the form Or any object that is submitting to action that has multiple AntiForgeryToken (This case will happen if we try to submit FormData or serialized data ) in case of ajax calls. This is a ASP MVC error it can happen while we creating forms.


For Example

<form id="dummy"  action="/Admin/Product/Create" method="post">

 @Html.AntiForgeryToken()

 <label  >Product Name</label>
 <input id="txtName" name="ProductName" type="text" placeholder="Product Name" >
 @Html.AntiForgeryToken()				
 <button id="btnSave" name="btnSave" >Save</button>									
         
</form>									

 

In the above example, we can see the form has two call to generate Anti Forgery Token so when we submit the form we will get the error “The anti-forgery token could not be decrypted”.

 

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 move second div to first in mobile view using css ?

we may need to show divs in different ways in mobile views. Like the same now consider a situation there is a requirement to move the second div to first and first div to second in mobile view. for web view it is normal.

You can find below code that helps to move second div to first and first div to second using css.

If you want to test

Step 1 : the code just copy the code
Step 2 : open a editor or notepad and paste the code
Step 3 : save the code in any location with file extension “.html” , Eg : index.html
Step 4 : open the file in a browser and check the mobile view.

Move second div to first and first div to second using css

<html>
<style>
.parent{
width:100%;
}

.parent div
{
width:50%;
float:left;

}
@media only screen and (max-width: 767px) {
   
    .parent 
    {   
       display: table;
    }
    .parent div{width: 100%;float: none}
    .leftColumn
    {
        display:table-footer-group; 
  color:red;
     
  
    }
    .rightColumn
    {
        display: table-header-group;
      
    } 
 
 }
</style>
<body>

<div class="parent">
 <div class="leftColumn">
 This is the first div
 </div>

 <div class="rightColumn">
 This is the second div
 </div>
<div>

</body>

</html>

 

 

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

Array in Javascript

Array is a variable that is used to hold more than one value in a single variable. That is we can hold a list of data inside an array.

How to declare an array in javascript ?

We can declare an array in two ways. these are the ways we can achieve it.

Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayName2 = ["data 1", "data 1", "data 3"];

How to add elements to array in javascript ?

Javascript array has a method push, the method we can use to add new elements to the array. when we use push the element will be added at the end of the array.
Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3
arrayName1.push("data 4");
var arrayNewLength=arrayName1.length; // output will be 4

How to delete an elements from array in javascript?

Javascript array has method named pop , it help us to delete an element from an array. when we use pop the elements at the end will be deleted.

Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3
arrayName1.pop();
var arrayNewLength=arrayName1.length; // output will be 2

How to find number of elements in an array in javascript ?

Javascript array has a property called length , the length property help us to find the number of elements exist in an array.
Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3