How to use URL Re-Write with Query String - asp.net

I am trying to create a URL re-write with a query string but my problem is the URL re-write does not work when I try to use the query string. The URL re-write works fine when I use it without the query sting. I have searched online and did exactly what they said but I was still getting an error. So let me show you what works first which is without the query-string:
This is what I have in the Global.asax file:
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("RouteForCustomer", "MyTest", "~/Users/MyOldPage.aspx");
}
Everything works fine if I run the code above.
So I have tried now to pass query-string in the URL and this what I have done that does not work so far:
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("RouteForCustomer", "MyTest/{My_ID}", "~/Users/MyOldPage.aspx");
}
And the code behind for MyOldPage.aspx I have this code but not sure if it is even necessary but this is what I saw online when I was researching:
protected void Page_Load(object sender, EventArgs e)
{
string myquerystring = Page.RouteData.Values["My_ID"] as string;

Related

ASP.NET initialization of array

I have website with array (list) of 1000 objects, these objects are loading from json to array every website refresh. I would like to load these objects from json to array only once and keep it in RAM for others users. Because everytime read file is much slower than read it from RAM.
I am using ASP.NET Web Forms
How is it posssible?
I would recommend to define the array as an static member of a class and then initialize it with help of Global.asax, use the Application_Start event handler.
to add Global.asax to you project in Visual Studio:
File -> New -> File -> Global Application Class
Here is a sample C# code for Global.asax.cs:
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
// ... Your initialization of the array done here ...
}
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 Application_End(object sender, EventArgs e)
{
}
}
Are these values static, i.e., do they stay constant while your application is running? In that case, the easiest way is to cache those values.
You can use static variables for that, but the recommended way is to use the thread-safe Cache object provided by ASP.NET. It can be accessed with the Cache property of the Page or of the HttpContext.
Example:
var myList = (MyListType)Cache["MyList"];
if (myList == null)
{
myList = ...; // Load the list
Cache["MyList"] = myList; // Store it, so we don't need to load it again next time.
}
Further reading:
Caching Application Data

Base Page/Derived Page event model in ASP.NET

I have written the following code in ASP.NET
I have a base page:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Base Page Called");
}
I have a derived page which have following code:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Derived Page Called");
}
Now while I am calling the Derived page it doesn't call Base Page's Page_Load. It displays
"Derived Page Called".
Now if I change the Derived page Load event handler name to "Page1_Load" and the implementation as following, the Base page is called.
protected void Page1_Load(object sender, EventArgs e)
{
Response.Write("Derived Page Called");
}
"Base Page Called".
What is the reason for this kind of behaviour?
Page_Load is automatically wired up if there exists a method with the Page_Load name, so if you define one in the derived class it will hide the one from the base. However, it has to match by name, so by giving the one in the derived class a suffix, it no longer hides the base implementation, so it will pick up the base one and use it.
If you put Page1_Load in the base as well, you will get no output
Base:
protected virtual void Page_Load(object sender, EventArgs e)
{
Response.Write("Base Page Called");
}
Derived:
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load();
Response.Write("Derived Page Called");
}
Try the above if you want both to be called

How to use ServiceStack Logging but have it delivered through the IOC container

Title about sums up what I'm looking to achieve, although now that I'm posting some code I would also like to know if the LogFactory in the correct place.
Thank you,
Stephen
public class ContactAppHost : AppHostBase
{
//Tell Service Stack the name of your application and where to find your web services
public ContactAppHost() : base("Contact Web Services", typeof (ContactService).Assembly)
{
// Built into the framework without the IOC
LogManager.LogFactory = new NLogFactory();
}
public override void Configure(Funq.Container container)
{
//register any dependencies your services use, e.g:
container.Register<ICacheClient>(new MemoryCacheClient());
}
}
protected void Application_Start(object sender, EventArgs e)
{
new ContactAppHost().Init();
}
protected void Application_Error(object sender, EventArgs e)
{
}
ServiceStack only supports the configuration of a single logger which should ideally be specified before the AppHost is initialized, so all static ILog initalizers for all classes in ServiceStack use the configured logger, e.g:
protected void Application_Start(object sender, EventArgs e)
{
LogManager.LogFactory = new NLogFactory();
new ContactAppHost().Init();
}

How does Global.asax PostAuthenticateRequest event binding happen?

How can I use the PostAuthenticateRequest event of Global.asax? I'm following this tutorial and it mentions that I have to use the PostAuthenticateRequest event. When I added the Global.asax event it created two files, the markup and the code-behind file. Here is the content of the code-behind file
using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace authentication
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
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 Application_End(object sender, EventArgs e)
{
}
}
}
Now when I type the
protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
It is successfully called. Now I want to know how is the PostAuthenticateRequest bound to this Application_OnPostAuthenticateRequest method? How can I change the method to some other?
Magic..., a mechanism called Auto Event Wireup, the same reason you can write
Page_Load(object sender, EventArgs e)
{
}
in your code-behind and the method will automatically be called when the page loads.
MSDN description for System.Web.Configuration.PagesSection.AutoEventWireup property:
Gets or sets a value indicating whether events for ASP.NET pages are automatically connected to event-handling functions.
When AutoEventWireup is true, handlers are automatically bound to events at run time based on their name and signature. For each event, ASP.NET searches for a method that is named according to the pattern Page_eventname(), such as Page_Load() or Page_Init(). ASP.NET first looks for an overload that has the typical event-handler signature (that is, it specifies Object and EventArgs parameters). If an event handler with this signature is not found, ASP.NET looks for an overload that has no parameters. More details in this answer.
If you wanted to do it explicitly you would write the following instead
public override void Init()
{
this.PostAuthenticateRequest +=
new EventHandler(MyOnPostAuthenticateRequestHandler);
base.Init();
}
private void MyOnPostAuthenticateRequestHandler(object sender, EventArgs e)
{
}

ASP.NET: Get Page's filename

I have an ASPX page named Default.aspx. From its codebehind on Page_Load(), I would like to get "Default.aspx", alone, into a string:
protected void Page_Load(object sender, EventArgs e)
{
string aspxFileName = ?;
}
What should I replace ? with—what will get me the ASPX filename?
System.IO.Path.GetFileName(Request.PhysicalPath);
protected void Page_Load(object sender, EventArgs e)
{
string cssFileName = Path.GetFileName(this.Request.PhysicalPath).Replace(".aspx", ".css");
}
Some short answers are already taken so, for fun, and because you'll likely want to do this from other Web Forms, here's an expanded solution that will affect all Web Forms in your project uniformly (includes code to get a filename as requested).
Make an extension method for the System.Web.UI.Page class by putting this code in a file. You need to use .NET 3.5.
namespace MyExtensions {
using System.Web.UI;
static public class Extensions {
/* You can stuff anybody else's logic into this
* method to get the page filename, whichever implementation you prefer.
*/
static public string GetFilename(this Page p) {
// Extract filename.
return p.AppRelativeVirtualPath.Substring(
p.AppRelativeVirtualPath.IndexOf("/") + 1
);
}
}
}
To get the filename from any ASP.NET Web Form (for example in the load method you specified):
using MyExtensions;
protected void Page_Load(object sender, EventArgs e) {
string aspxFileName = this.GetFilename();
}
Call this method on any Web Form in your project.

Resources