Attribute Routing in ASP.NET MVC 5
--------------------------------------------------------------------------------------------------------------------------
1) overload controller methods in ASP.NET MVC?
- Routing is how ASP.NET MVC matches a URI to an action. MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web application.
- The earlier style of routing, called convention-based routing, is still fully supported. In fact, you can combine both techniques in the same project.
Why Attribute Routing?
For example, a socially enhanced e-commerce website could have the following routes:
{productId:int}/{productTitle}
Mapped toProductsController.Show(int id)
{username}
Mapped toProfilesController.Show(string username)
{username}/catalogs/{catalogId:int}/{catalogTitle}
Mapped toCatalogsController.Show(string username, int catalogId)
(Don’t mind the specific syntax right now, we will touch on this later.)
In previous version of ASP.NET MVC, the rules would be set in the
RouteConfig.cs
file, and point to the actual controller actions, as such:- routes.MapRoute(
- name: “ProductPage”,
- url: “{productId}/{productTitle}”,
- defaults: new { controller = “Products”, action = “Show” },
- constraints: new { productId = “\\d+” }
- );
When the route definitions are co-located with the actions, within the same source file rather than being declared on an external configuration class, it can make it easier to reason about the mapping between URIs and actions. The previous route definition would be set using the following, simple attribute:
- [Route(“{productId:int}/{productTitle}”)]
- public ActionResult Show(int productId) { … }
Enabling Attribute Routing
To enable attribute routing, call
MapMvcAttributeRoutes
during configuration.- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
- routes.MapMvcAttributeRoutes();
- }
- }
You can also combine attribute routing with convention-based routing.
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
- routes.MapMvcAttributeRoutes();
- routes.MapRoute(
- name: “Default”,
- url: “{controller}/{action}/{id}”,
- defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
- );
- }
--------------------------------------------------------------------------------------------------------------------------
1) overload controller methods in ASP.NET MVC?
Polymorphism is a part of C# programming while HTTP is a protocol. HTTP does not understand polymorphism. HTTP works on the concept's or URL and URL can only have unique name's. So HTTP does not implement polymorphism.
In order to fix the same we need to use "ActionName" attribute.
public class CustomerController : Controller
{
//
// GET: /Customer/
public ActionResult LoadCustomer()
{
return Content("LoadCustomer");
}
[ActionName("LoadCustomerbyName")]
public ActionResult LoadCustomer(string str)
{
return Content("LoadCustomer with a string");
}
}
So now if you make a call to URL "Customer/LoadCustomer" the "LoadCustomer" action will be invoked and with URL structure "Customer/LoadCustomerByName" the "LoadCustomer(string str)" will be invoked.
2) Difference between hashtable and dictionary
A) .The Hashtable object contains items in key/value pairs. The keys are used as indexes, and very quick searches can be made for values by searching through their keys.
.Temporary tables are a useful tool in SQL Server provided to allow for short term use of data. There are two types of temporary table in SQL Server, local and global.
3) Difference between Unique and primary key
Primary Key
- Primary key cannot have a NULL value.
- Each table can have only one primary key.
- By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
- Primary key can be related with another table's as a Foreign Key.
- We can generated ID automatically with the help of Auto Increment field. Primary key supports Auto Increment value.
Unique Key
- Unique Constraint may have a NULL value.
- Each table can have more than one Unique Constraint.
- By default, Unique key is a unique non-clustered index.
- Unique Constraint can not be related with another table's as a Foreign Key.
- Unique Constraint doesn't supports Auto Increment value
Foreign Key
- Foreign key is a field in the table that is primary key in another table.
- Foreign key can accept multiple null value.
- Foreign key do not automatically create an index, clustered or non-clustered. You can manually create an index on foreign key.
- We can have more than one foreign key in a table.
4) Group By Department and get number of employees present in that department
A) var groupedEmploeesByDepartment = empdep.GroupBy(x => x.Department)
.Select(x => new { Department = x.Key, EmployeesCount = x.Count() });
Superb i really enjoyed very much with this article here. Really its a amazing article i had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.
ReplyDeleteBest Dental Clinic In Chennai
Best Dental Clinic In Tambaram