.NET 4.0 routes and application_start
right i'm using the application_start event in the Global.asax like so:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
开发者_Python百科 RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
var context = new QuestionEntities();
var questionquery = from c in context.Questions where c.Alias != null select c;
string alias = "";
foreach (Question q in questionquery.ToList())
{
try
{
alias = q.QuestionText.Replace(" ", "-").Replace("?", "").ToLower();
}
catch { }
routes.MapPageRoute("", alias, "~/Default.aspx");
}
routes.MapPageRoute("", "home", "~/Default.aspx");
routes.MapPageRoute("", "rss", "~/rss.aspx");
}
This is all fine, but when I create a new question and add an alias, this doesn't refire. I've tried stopping and starting the website. Is there a way of forcing this event to refire.
I'm using reinvent hosting so don't have direct access to IIS to recyle it.
Thanks for your help Rob
Application_Start only first the first request after your app pool starts. The app pool will restart on a change in the bin folder or the web.config. Otherwise though your code won't fire.
To do what you want you'll probably need an IHttpModule, or something else that runs on each request.
Alternately a catchall route might be what you want. Use {*catchall}
for the path.
精彩评论