What is wrong with my .net routes? - asp.net

I have followed a few tutorials online and they all seem to show the same logic for .net routing using ASP.net web forms. When I execute the URL below I get a 404 error. Test.aspx is in the root folder of this application.
http://www.mydomain.com/member/abc
Here is my global.asax contents:
<%# Application Language="C#" %>
<%# Import Namespace="System.Web.Routing" %>
<script runat="server">
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"TestABC",
"member/{name}",
"~/Test.aspx");
}
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
Is there something I need to do with my web.config file?
Any help is greatly appreciated.

I'm guessing that your routing module is not triggered when you hit the iis server.
As a test to verify that this is the cause : change your webconfig to run all managed modules upon a request.
You need to set this :
<modules runAllManagedModulesForAllRequests="true">
If that solved it you can go read this resource on why to not do that :)

Related

How to handle Application_AcquireRequestState, Session_Start and Session_End in ASP.NET 4.5 using OWIN?

We use ASP.NET 4.5 (VS 2013) and want to replace Global.asax.cs with new Startup.cs file, which comes from OWIN specification.
We need to replace Application_AcquireRequestState, Session_Start and Session_End handlers with something in Startup.cs file. It looks as following in Global.asax.cs:
protected void (Object sender, EventArgs e)
{
SessionCounter.AddSessionPage(Context);
}
protected void Session_Start(Object sender, EventArgs e)
{
}
protected void Session_End(Object sender, EventArgs e)
{
LoginLog.RegisterLogOff(Context);
SessionCounter.AbandonSession(Context);
}
How can we do that?
OWIN has no definition for session and cannot fully replace the Global.asax.cs file.
Try ASP.NET 5, it moves everything from Global.asax.cs to Startup.cs. https://github.com/aspnet/home

Is it possible to handle events in ASP.NET app that uses OWIN?

We have ASP.NET application with Global.asax.cs file where we use application event handlers as following:
protected void Application_Start(object sender, EventArgs e)
{
log.Info("The App started.");
}
protected void Application_End(object sender, EventArgs e)
{
log.Info("The App finished.");
}
We want to use OWIN now, which means adding Startup.cs file. Is there any way to move handlers from Global.asax.cs то Startup.cs file or something else where we can put our logs?
OwinContext in Startup.Configuration() is different from the traditional ASP.NET HttpContext which exists in MvcApplication.Application_Start(). Both are using different pipelines.
And because of that you can't use MvcApplication.Application_Start() in Startup.Configuration()

ASP.NET WebPages use html extension

I'm trying to use ASP.NET WebPages to make sense of an existing site which uses static .html files (about 500 of them). Unfortunately, my SEO person is requiring that the site maintains its existing directory / filenames, so I need to use .html.
After finding this example, I tried adding the extension in web.config under compilation/buildProviders/ as:
<add extension=".html" type="System.Web.WebPages.Razor.RazorBuildProvider"/>
And adding an Assembly as well:
<add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
This still doesn't render the page. It is simply output as source. I also created a Global.asax at the root of the project and added this under Application_Start():
System.Web.Razor.RazorCodeLanguage.Languages.Add(
"html", new System.Web.Razor.CSharpRazorCodeLanguage());
System.Web.WebPages.WebPageHttpHandler.RegisterExtension("html");
Still had no effect. Unfortunately, I'm drawing a blank on Google.
I happened upon this question while trying to solve the same problem - although in my case, for curiosity's sake.
Here's what you need in your web.config file:
<system.web>
<compilation>
<buildProviders>
<add extension=".html"
type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>
</buildProviders>
</compilation>
</system.web>
<system.webServer>
<handlers>
<add name="Html" verb="*" path="*.html"
type="System.Web.Webpages, WebPageHttpHandler"/>
</handlers>
</system.webServer>
This isn't enough on its own, though! We need to register the extension with WebPageHttpHandler.
Normally, you'd be able to do stuff like this in the _AppStart file - unfortunately, when the application starts (i.e when _AppStart executes), it iterates over the items in the SupportedExtensions of WebPageHttpHandler, so we can't actually register the extension in AppStart.
What I did is I made a new .dll assembly with the PreApplicationStartMethod attribute, as seen here, but you can also do it inside the Global.asax file's Application_Start method.
Finally, we also need to add "html" as an extension to the RazorCodeLanguage.Languages dictionary, so that the Razor engine can figure out how to compile the template.
Example Global.asax file:
<%# Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
System.Web.WebPages.WebPageHttpHandler.RegisterExtension("html");
var languages = System.Web.Razor.RazorCodeLanguage.Languages;
languages.Add("html", languages["cshtml"]);
}
</script>
You want to use routing. Are you using webforms or MVC?
Global.asax is a good start. Add the complete code here:
namespace Name
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Route1", "OldPage.html", "~/NewPage.aspx");
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
Obviously you don't want to manually add 500 routes but you can add url filters.
See: http://msdn.microsoft.com/en-us/library/cc668201.ASPX
ASP.NET routing enables you to use URLs that do not have to map to
specific files in a Web site.

url rewriting in asp.net 4.0: A route named 'xxxxx' could not be found in the route collection

The error I'm getting is
A route named 'xxxxx' could not be found in the route collection.
Parameter name: name
global asax
<%# Application Language="C#" %>
<%# Import Namespace="System.Web.Routing" %>
<script runat="server">
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("test2-testurl", "test2/{category}", "~/test2.aspx");
}
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
In page test1 I have coded below like this on button click event:
protected void Button1_Click(object sender, EventArgs e)
{
string url = Page.GetRouteUrl("test2-testurl", new { category = "laptops" });
Response.RedirectToRoute(url);
}
Maybe try this (remove 'test2'):
routes.MapPageRoute("test2-testurl", "{category}", "~/test2.aspx");
Update:
try just to redirect to page this way
Response.Redirect("page2/laptops");
Example:
// add route
routes.MapPageRoute("ad-view", "view/{ad-id}", "~/ad_view.aspx");
// on page I usualy bind values in repeater to links like this:
<asp:HyperLink ID="TitleHL" NavigateUrl='<%# "view/" + Eval("ad_id") %>' runat="server">
// to redirect from code-behind i'll use:
Responce.Redirect("view/" + ad_id);

Routing and Ninject gives 404

I'm trying to setup routing in my web application.
It doesn't seem to work with Ninject however. If I comment all the Ninject in my Global.asax, the route works like a charm.
With Ninject in the file, I just get a 404 "The resource cannot be found" when trying to open the route page.
Heres what is in my Global.asax code:
<%# Application Language="C#" Inherits="Ninject.Web.NinjectHttpApplication" %>
<%# Import Namespace="Infrastructure.Storage" %>
<%# Import Namespace="Ninject" %>
<%# Import Namespace="Ninject.Modules" %>
<%# Import Namespace="System.Web.Routing" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel(new SiteModule());
return kernel;
}
public class SiteModule : NinjectModule
{
public override void Load()
{
//Bind<ILogger>().To<NLogger>().InSingletonScope();
//Bind<IAuthentication>().To<Authentication>();
Bind<ISession>().To<LinqToSqlSession>();
Bind<IReadOnlySession>().To<LinqToSqlReadOnlySession>();
//Bind<IReporting>().To<LinqToSqlReporting>();
}
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("City", "Cities/{id}", "~/test2.aspx");
}
</script>
Anyone have an idea for what could be wrong?
When you use the NinjectHttpApplication class for your asax, you need to change the way the applicationStart & applicationEnd are called.
What's happening is .net automatically wires the methods above to the corresponding events. Because the NinjectHttpApplication already handles the Application_Start, your method won't get called, hence your routes are not registered. You need change that method to
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
RegisterRoutes(RouteTable.Routes);
}

Resources