Once the ASP. NET MVC application is deployed to the line, it cannot modify the routing configuration, only to recompile the application, redeploy to the Assembly to complete, and recently read a blog about how to change the routing options in order to run directly into the deployed application.
First look at me in the program, the solution structure options, easy to learn, the route folder is placed in the required class file and interface, in the controller I defined a homecontroller in the inside defines the index and modify two methods, The corresponding view is also defined in the Views folder for test use.
1, define a IRouteRegistrar.cs interface for writing the Routes.cs class
<summary>
Routing Registration Interface
</summary>
public interface Irouteregistrar
{
void Registrarroutes (routecollection routes);
}
2. Write a Routes.cs class and implement the Irouteregistrar interface
<summary>
Route configuration file to change the route to change in this file
</summary>
public class Routes:irouteregistrar
{
public void Registrarroutes (routecollection routes)
{
Implement the method of the route registration, the method is written and RoutesConfig.cs the same way
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");
Routes. MapRoute (
Name: "Default",
URL: "{controller}/{action}/{id}",
defaults:new {controller = "Home", action = "Index", id = urlparameter.optional}
);
}
}
3. Write routes extension class RouteRegistrationExtensions.cs for routes collection extensions for easy follow-up operations
<summary>
Extended route registration method to facilitate subsequent code writing
</summary>
public static Class Routeregistrationextensions
{
<summary>
Load route
</summary>
<param name= "Routes" > Routing objects to be extended </param>
<param name= "virtualpath" > File relative path exists </param>
static void Reloadroutes (this routecollection routes, string virtualpath)
{
To create an assembly from a file
var assembly = buildmanager.getcompiledassembly (virtualpath);
Reflecting class objects based on class name note: Class names need to be prefixed with the namespace of the class name
var registrar = assembly. CreateInstance ("MVC routing configuration can be changed after publishing Demo.routes") as Irouteregistrar;
using (routes. Getwritelock ())
{
Routes. Clear ();
if (Registrar!=null)
{
Registering routes
Registrar. Registrarroutes (routes);
}
}
}
//<summary>
//Registration route
//</summary>
//<param name= "Routes" > Route object to be extended </param>
//<param Name= "VirtualPath" ></PARAM>
public static void RegisterRoutes (this routecollection Routes, string virtualpath)
{
if ( String.IsNullOrEmpty (virtualpath))
{
throw new ArgumentException ("virtualpath path does not exist");
}
routes. Reloadroutes (virtualpath);
//Invoke Listener events
Configfilechangenotifier.listen (virtualpath, routes. Reloadroutes);
}
}
4. Write a ConfigFileChangeNotifier.cs class that is used to listen for configuration file changes and to do the appropriate actions
<summary>
Configuration file Change Listener class
</summary>
public class Configfilechangenotifier
{
VirtualPathProvider _VPP;
Action<string> _changecallback;
Private Configfilechangenotifier (action<string> changecallback)
: This (Hostingenvironment.virtualpathprovider, changecallback)
{ }
Private Configfilechangenotifier (VirtualPathProvider VPP, action<string > Changecallback)
{
_VPP = VPP;
_changecallback = changecallback;
}
<summary>
Listen for events (call action when the contents of a file change)
</summary>
<param name= "virtualpath" > File path </param>
<param name= "Action" > Delegate Object </param>
public static void Listen (String virtualpath, action<string> Action)
{
var notifier = new Configfilechangenotifier (action);
Notifier. Listenforchanges (virtualpath);
}
void Listenforchanges (String virtualpath)
{
var virtualpathdependencies = new list<string> ();
Virtualpathdependencies.add (virtualpath);
CacheDependency cachedependency = _vpp. Getcachedependency (
VirtualPath, Virtualpathdependencies, Datetime.utcnow);
HttpRuntime.Cache.Insert (VirtualPath/*key*/,
VirtualPath/*value*/,
CacheDependency,
Cache.noabsoluteexpiration,
Cache.noslidingexpiration,
Cacheitempriority.notremovable,
New CacheItemRemovedCallback (onconfigfilechanged));
}
void onconfigfilechanged (String key, Object value,
cacheitemremovedreason reason)
{
//Determine if changes have occurred
if (reason! = cacheitemremovedreason.dependencychanged)
{
Return
}
_changecallback (key);
listenforchanges (key);
}
}
5. The required tool class has been written, and in the end you need to find the RouteConfig.cs class in the App_start folder, and in the RegisterRoutes method, register the code you just wrote
public static void RegisterRoutes (RouteCollection routes)
{
RouteTable.Routes.RegisterRoutes (@ "~/route/routes.cs");
}
Well, the above code is written, and it works as follows.
Run the program This is the effect in the original routing situation, do not close the running application, which is found in the application in the route folder under the Routes.cs with Notepad open, change the routing configuration, re-refresh in the browser, the default load of the view will change. At this point, you achieve the effect you want.
Editable Route-asp.net MVC Project compilation, modifying the routing configuration to dynamically load