SoFunction
Updated on 2025-03-01

C# How to cancel the selection of backup controller in MVC3

The C# MVC searches for the corresponding controller first, and first, it is to find the current domain.

If you can't find it, you will look for a spare one..

But sometimes we don't want him to look for a backup controller.

This involves a DataToken

Copy the codeThe code is as follows:

(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = }, // Parameter defaults
new string[] { "" }
);

The first time I used new string to distinguish, but unfortunately he still searched. After reading some information, I

I found that if you want to cancel the backup controller, you need to write this

Copy the codeThe code is as follows:

(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = }, // Parameter defaults
new string[] { "" }
).DataTokens["UseNamespaceFallBack"]=false;

In this way, we cancel the operation of using the backup controller.


Also back up some MVC conventions
Copy the codeThe code is as follows:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// = false;
// = false;
<PluralizingTableNameConvention>();//Remove the contract for plural table names
<IncludeMetadataConvention>();
(modelBuilder);
/*

Conventions that can be deleted are:
Namespace:
• AssociationInverseDiscoveryConvention
Look for properties of classes referenced to each other on the navigation and configure them to the same relationship of inverse properties.
• ComplexTypeDiscoveryConvention
Look for types that have primary keys and configure them as complex types.
• DeclaredPropertyOrderingConvention
Ensure that each entity's main key attributes take precedence over other attributes.
• ForeignKeyAssociationMultiplicityConvention
Is the configuration required or optional relationship based on the null foreign key attribute if included in the class definition.
• IdKeyDiscoveryConvention
Look for properties named Id or <TypeName> Id and configure them as primary keys.
• NavigationPropertyNameForeignKeyDiscoveryConvention
Using foreign key relationships, use the <NavigationProperty> <PrimaryKeyProperty> mode as the appearance of the property.
• OneToManyCascadeDeleteConvention
The required relationship is stacked on the switch.
• OneToOneConstraintIntroductionConvention
Will be configured as a: A relational foreign key primary key.
• PluralizingEntitySetNameConvention
The name of the entity set in the entity data model configured as a diversified type name.
• PrimaryKeyNameForeignKeyDiscoveryConvention
Use foreign key relationships and use the <PrimaryKeyProperty> mode as the appearance of the property.
• PropertyMaxLengthConvention
Configure all string and byte [] properties, with maximum length by default.
• StoreGeneratedIdentityKeyConvention
Configuration by default, the primary key that identifies all integers.
• TypeNameForeignKeyDiscoveryConvention
Using a foreign key relationship, use the <PrincipalTypeName> <PrimaryKeyProperty> mode as the appearance of the property.


*/}