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 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.

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”.

 

What are the different action method’s return type?

1) ViewResult : Used to return a view to render html in the browser. this is the most common used action result

2)PartialViewResult: Similar to view result, it returns a partial view.

3) ContentResult: Used to return any type of content. By default it is used to return plain text but the actual content type can be explicitly defined

4) EmptyResult: this is equivalent to void method,it is by definition an action result object,but it does nothing.

5)FileResult:Used to return binary content.

6)HttpUnauthroizedResult: You can return an HttpUnauthrized result when the request tries to access restricted content that, for example its not available to anonymous user.

7)JavaScriptResult:Used to  return JavaScript code.

8)JsonResult: Used  to return any object in JavaScript Object Notation(JSON) format.

9)RedirectRresult:Used to perform an HTTP redirect to another URL.you can define it as temporary (code 302) or permanent (code 301)  HTTP redirect.

10)RedirectToRouteResult: used to perform an HTTP redirect ,but to specific route rather than a URL

 

 

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