Alternative Query String C# (asp.net) - asp.net-2.0

Is there a quick and dirty way of using a query passed as follows:
domain.com/mypage.aspx/product/toycar/
I've done it before in PHP, but this needs to be done in page (in this instance).
--
I only have access to the aspx page and code behind, and have to work in asp.net 2 (i wish i was using 3.5)

quick and dirty:
public class ModuleRewriter : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
// The url will look like: http://domain.com/mypage.aspx/product/toycar/
// The module will rewrite it to: http://domain.com/mypage.aspx?product=toycar
HttpApplication application = source as HttpApplication;
string[] urlInfo = application.Request.RawUrl.ToString().Split('/');
if (urlInfo.Length > 2)
{
string page = urlInfo[urlInfo.Length - 3];
string action = urlInfo[urlInfo.Length - 2];
string id = urlInfo[urlInfo.Length - 1];
if (string.IsNullOrEmpty(page))
{
page = "default.aspx";
}
application.Server.Transfer(string.Format(
"~/{0}?{1}={2}", page, action, id));
}
}
public void Dispose()
{
}
}
web.config:
<httpModules>
<add name="ModuleRewriter" type="ModuleRewriter, MyWebApplication"/>
</httpModules>
and a test page:
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= Request["product"] %>
</div>
</form>
</body>
</html>

You might want to look into the ASP.NET System.Web.Routing namespace, which was added in .NET 3.5 SP1 I believe:
http://blogs.msdn.com/mikeormond/archive/2008/05/14/using-asp-net-routing-independent-of-mvc.aspx
http://msdn.microsoft.com/en-us/library/system.web.routing.aspx
You'd be able to get rid of the .aspx extension too.

This would involve making a custom HTTP Handler.
Check this

You've got a few options, but all of them require access to the web.config and a change to IIS to map all file extensions to the dotNet ISAPI dll:
Use MVC (like stackoverflow does,
notice the urls)
Use asp.net routing (new in 3.5)
Write your own http handler Massive guide from Microsoft here
Use the excellent urlrewriting.net which does just about everything perfectly including getting round some awkward authentication and image path problems.
Personally I used urlrewriting.net with good results.
Since you mention you don't have access to anything but the code behind and the page, the only thing I can think of is actually creating those dirs (if you have access to do that) and using a server.transfer page passing the value to your actual page in the folder above. Messy, but if you can't access the other stuff, your choices are limited.

In case you just want to read the path from within your .aspx:
Request.ServerVariables["PATH_INFO"]
To clarify:
he has only access to the aspx (+ codebehind) itself, so he must know how the query is, but it is not in the Request.QueryString because of the format. So the only way then is Request.ServerVariables["PATH_INFO"] (Request.RawUrl)

Related

Failed to Parse Manifest : Using asp.net

I am following Stephen Walther's guide and everything builds without errors. However once I run the application in Chrome I get this error message:
Application Cache Error event: Failed to parse manifest http://localhost/website/Manifest.ashx
And nothing is cached.
From what I have gathered from here, I have a type-o in my manifest. Maybe you can see something I did wrong and causing this error message.
Manifest.ashx:
<%# WebHandler Language="C#" Class="JavaScriptReference.Manifest" %>
using System;
using System.Web;
namespace JavaScriptReference {
public class Manifest : IHttpHandler {
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/cache-manifest";
context.Response.WriteFile(context.Server.MapPath("Manifest.txt"));
}
public bool IsReusable {
get {
return false;
}
}
}
}
Manifest.txt:
CACHE MANIFEST
CACHE:
Images/img1.jpg
Images/img2.jpg
JScript.js
Default.aspx.vb
# Does Default.aspx.vb even need to be cached?
TLDR: Don't add a CACHE: entry in your manifest, don't cache code-behind files and make sure you registered the HttpHandler in your Web.Config
Long Version:
There are a few things that you need to do to make the sample app work. First up you create your handler as above, an example in C# is:
using System.Web;
namespace CacheTest
{
public class Manifest : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/cache-manifest";
context.Response.WriteFile(context.Server.MapPath("Manifest.txt"));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Next you need to register the handler in your web.config like:
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="Manifest.ashx"
type="CacheTest.Manifest, CacheTest" />
</httpHandlers>
</system.web>
</configuration>
Next up create a Manifest.txt in the root of your website and populate it. The sample should not have a CACHE: heading inside it. A working sample may look like:
CACHE MANIFEST
# v30
Default.aspx
Images/leaping-gorilla-logo.png
Note that we do not cache code behind files, only relative paths to actual resources that a browser may request. Finally, add a Default.aspx file. Ignore the code behind but edit the markup so that the initial HTML tag references the HttpHandler, the full markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CacheTest.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" manifest="Manifest.ashx">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
This is a sample offline app!
</div>
</form>
</body>
</html>
With this done you can now start your website, browse to it in FireFox and you will be asked permission to take it offline. Alternatively, fire it up in Chrome, switch to the developer tools, check the Resources tab and you will be able to see the resources that have been loaded under the Application Cache node:
And for completeness, your finished code structure will look like:
The error "Application Cache Error event: Failed to parse manifest" can be caused by formatting of the text file.
My deployment script generated the manifest file in Unicode. The file looked fine in Chrome (when going to the URL), validated on online validators, but would generate this error when being used as a manifest.
To fix the file, just open the manifest file in notepad and go to "Save-As" and select UTF8.

Creating website template which is independent from source code in asp.net webforms

Im working on an e-commerce platform which supports multiple stores on different domains. The backend is complete (for now) and I'm now starting the front-end part. Since the platform supports multiple stores I need some kind of templating system and I haven't quite figured out what's the best way to do this.
This are my requirements:
I don't want to rebuild my solution to add a new template
Templates are stored in /Templates/TemplateDirectoryName
I want to be able to use (predefined) usercontrols inside the templates.
I use URL routing and only have 1 Default.aspx page which controls which page template and additional code needs to be loaded.
This is what I came up with so far:
In my template directory i have templates created with master pages (A homepage master file, a default master file, and sub-master files referencing the default master file...for product detail, browse, search etc)
My Default.aspx page picks the right template based on routing values
While this way works I don't think it's very practical but the more I think about it the more I come to the conclusion that there are not so many other options to go around this. I think this is what I want to ask: Is there a way to use usercontrols in a template and have the template completely seperated from the application so users can create templates without having to worry about the site's namespace and structure?
Kind regards,
Mark
Since you are referencing a folder for templates, wouldn't it be possible to just change the .aspx files in the folder and asp.net shall pick up the template based on the url path that you've mentioned? I think that is possible in asp.net.
Also, Frameworks like DotNetNuke, Sharepoint, Joomla etc. have the similar concept. You can avail their features.
My proposed solution is below. It has a few constraints, like all master pages need to implement the same set of placeholder controls (not surprising). Take a look and let me know what you think.
I setup my folder structure like this:
Website -> Templates -> TemplateFolder (named same as the template)
Website -> Templates -> UserControls (User controls are stored in a non-template specific folder)
I defined a simple Template configuration class which we can store/save/load a basic template deffinition:
public class Template
{
public string TemplateName { get; set; }
public string UserControlName { get; set; }
public string MasterPageName { get; set; }
public string TemplateFolder
{
get
{
return GetTemplateFolder(TemplateName);
}
}
public string TemplateConfigFile { get { return GetTemplateConfigFile(TemplateName); } }
private static string GetTemplateFolder(string name)
{
return HttpContext.Current.Server.MapPath("~/Templates/" + name + "/");
}
private static string GetTemplateConfigFile(string name)
{
return GetTemplateFolder(name) + "/" + name + ".config";
}
public Template()
{
}
public void Save()
{
XmlSerializer xs = new XmlSerializer(typeof(Template));
if (!Directory.Exists(TemplateFolder)) Directory.CreateDirectory(TemplateFolder);
using (FileStream fs = File.OpenWrite(TemplateConfigFile))
{
xs.Serialize(fs, this);
}
}
public static Template Load(string name)
{
if(!File.Exists(GetTemplateConfigFile(name))) return null;
XmlSerializer xs = new XmlSerializer(typeof(Template));
using (FileStream fs = File.OpenRead(GetTemplateConfigFile(name)))
{
Template t = (Template)xs.Deserialize(fs);
return t;
}
}
}
You can build some bare xml code to get started with by running the code below:
Template t1 = new Template() { TemplateName = "Template1", MasterPageName = "Child1.master", UserControlName = "uc1.ascx" };
Template t2 = new Template() { TemplateName = "Template2", MasterPageName = "Child2.master", UserControlName = "uc2.ascx" };
t1.Save();
t2.Save();
I created a basic master page. This page will probably never be used except to give your default page the basic placeholders. All of your master pages should have the same set of placeholders as your base one so that your pages can use them all interchangably. Notice I left a placeholder for our user control.
<%# Master Language="C#" AutoEventWireup="true" CodeFile="BaseMaster.master.cs" Inherits="Templates_Masters_BaseMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="cphHeader" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder id="cpUserControl" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder id="cphFooter" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Now I create a basic aspx web page that uses the above master page.
<%# Page Title="" Language="C#" MasterPageFile="~/Templates/Masters/BaseMaster.master" AutoEventWireup="true" CodeFile="DefaultTemplated.aspx.cs" Inherits="DefaultTemplated" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphHeader" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cpUserControl" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="cphFooter" Runat="Server">
</asp:Content>
In the codebehind we'll setup the basic templating. This sets the masterpage and adds a pre-defined user control to the content placeholder for user controls. If you wanted you could just make a panel or something and add it to that fixed control, but I thought you might appreciate seeing how to make it work with masterpages.
public partial class DefaultTemplated : System.Web.UI.Page
{
private Template PageTemplate
{
get
{
if (_tLoaded == null)
{
string template = Request.QueryString["template"];
if (string.IsNullOrEmpty(template)) return null;
Template t = Template.Load(template);
_tLoaded = t;
}
return _tLoaded;
}
}
private Template _tLoaded = null;
protected void Page_Load(object sender, EventArgs e)
{
if (PageTemplate != null)
{
//cpUserControl is the name of my usercontrol placeholder
((ContentPlaceHolder)Page.Form.FindControl("cpUserControl")).Controls.Add(
Page.LoadControl("~/Templates/UserControls/" + PageTemplate.UserControlName));
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
if (PageTemplate == null) return;
this.MasterPageFile = "~/Templates/" + PageTemplate.TemplateName + "/" + PageTemplate.MasterPageName;
}
}
If you had a template named "Template1" you could use it by calling "Default.aspx?template=Template1". Since you are using URL rewriting you would use the rewriting to pass the template name as a parameter to the page.
Another option that could be combined with the above would be the use of Page.ParseControl. Using this you could store your raw asp.net designer code (designer only) in a database or raw text file. Then you could instantiate it load it like this:
//where pnl1 is a Panel on the page. Page.ParseControl just returns a control object, so use it anywhere.
pnl1.Controls.Add(Page.ParseControl("raw asp.net designer code here."));
One great thing about this is that nested controls work great too.
I don't know if I understand correctly:
If you don't want to rebuild, then I can tell a CMS concept is best suitable for you.
You can store your templates as an HTML in database and from DB you can fetch it back,
You can give an admin functionality with editor to edit your template online also.

How to LoadControl a control that uses VaryByControl OutputCache, specifying values for properties

I've got a user control that should use caching, with VaryByControl. The .ascx file looks like this :
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="mynamespace.TestControl" %>
<%# OutputCache Duration="10" Shared="true" VaryByControl="Test" %>
<p id="SomeText" runat="server">Nothing</p>
The TestControl class in the code-behind file has a int Test {...} property and an Page_Load() event handler that fills the SomeText paragraph with:
SomeText.InnerText = string.Format(#"Test={0} at {1}", Test, DateTime.Now)
I've got a .aspx file that looks like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="mynamespace.TestPage" %>
<%# Register TagPrefix="xxx" TagName="TestControl" Src="Controls\TestControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<xxx:TestControl Test="6" runat="server" />
<xxx:TestControl Test="7" runat="server" />
<hr />
<asp:PlaceHolder ID="Suport" runat="server" />
</body>
</html>
The two <xxx:TestControl> tags properly load instances of TestControl with Test set to the expected value, I can refresh the browser a few times and I can see the cache properly doing it's job.
Now I'd like to fill the <asp:PlaceHolder ID="Suport" /> with some instances of TestControl, using varying Test values, that should all benefit from proper caching. I'm trying to use the LoadControl method, but I can't find a way to specify a value for the Test property. I expect such a method to exist, after all asp.net code loading the .aspx page manages to find the proper cached control. All I get is an instance of PartialCachingControl without CachedControl initialized and at runtime the rendered TestControl shows Test has the default value of 0.
This is how my .aspx Page_Load() event handler looks like:
protected void Page_Load(object sender, EventArgs e)
{
PartialCachingControl tc = (PartialCachingControl) LoadControl(#"Controls\TestControl.ascx");
if (tc.CachedControl != null)
((TestControl)tc.CachedControl).Test = 67;
Suport.Controls.Add(tc);
}
Edit
I could work around the problem by caching the whole page, but it just seems odd that I can't find a way to do it this way. Especially since invoking the control through the ASPX file works as expected (proving there's a way).
Edit 2
Hmm, no answers so far. I started a bounty, hopefully it gets a bit more attention.
To get a control participate in the full page life cycle it shall be added in the Init event or the CreateChildControls method rather than to add it on Load. Since VaryByControl needs fully qualified control identifiers to work it must be initialized before the page cycle begins.
Something similar to this:
protected override void OnInit(EventArgs e) {
var testControl = LoadControl(#"TestControl.ascx");
testControl.ID = "TestControl";
Suport.Controls.Add(testControl);
base.OnInit(e);
}
protected override void OnLoad(EventArgs e) {
TestControl testControl = GetTestControl("TestControl");
if(testControl != null){ //If it is null it is cached and can not be changed
testControl.Test = 242;
}
base.OnLoad(e);
}
private TestControl GetTestControl(string name) {
var control = this.Suport.FindControl(name);
var partialCachedControl = control as PartialCachingControl;
if(partialCachedControl != null) {
control = partialCachedControl.CachedControl;
}
return control as TestControl;
}
Since the output is cached per control you can not change the control until the cache is cleared. If you want to change the value and regenerate the content you either have to get the cache cleared or create a new control (with a new ID). One way to clear the cache is to use VaryByCustom instead and generates a cache key that changes if your Test-value is changing.
Also remember to implement INamingContainer interface on your test-control to avoid naming conflicts between the different objects. To do this, just add the interface to the control, like this:
public class TestControl: WebControl, INamingContainer {}
You have to swap 2 lines in order to make your code work :
PartialCachingControl tc = (PartialCachingControl) LoadControl(#"Controls\TestControl.ascx");
Suport.Controls.Add(tc);
if (tc.CachedControl != null)
((TestControl)tc.CachedControl).Test = 67;
As soon as you add the control, the cached control is initialized.
E.G.
I think you have misunderstood the VarByControl-property, it does not tell the cache to change upon a property on the control, but on the ID of controls on the page. Here is the text from MSDN:
The VaryByControl property is set to fully qualified control identifiers, where the identifier is a concatenation of control IDs starting from the top-level parent control and delimited with a dollar sign ($) character.
In your case you can maybe set VaryByCustom instead of VaryByControl and generate a cache key from the Test-property-value and vary it if it changes.

Setting ResponseCode manually at CodeBehind fails with IIS7 and 2008 Server

I have developed a web app a year ago aimed to work with IIS6.
Now we are moving to IIS7 and I thought, I'd do some integration tests.
One of this fails:
The web app is more or less a search-engine, giving a 404 or 500 (thanks to your google-advisor ...) when there weren't any results or the data-container is not loaded yet. With IIS6 this worked great: The page output was eg. result.aspx, showing some message and giving back the specified http status (set at codebehind).
Now with IIS7 this behaviour is broken: If I set the http status code at codebehind, my page won't be delivered anymore - instead showing the generic error page of IIS7.
No, I do not want to do any dirty hack with the customErrors-Section ...
I just want the original behaviour back!
Is there any way to do this?
Edit:
Consider following page
<%# Page Language="C#" AutoEventWireup="true"%>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Response.StatusCode = 404;
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
This page should be displayed
</div>
</form>
</body>
</html>
Vista + IIS7 = OK
2008 Server + IIS7 = Generic Error Page
have you tried this:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
}
HttpResponse.TrySkipIisCustomErrors Property (System.Web)
Your problem is probably that you have tried to set the Status Code (which counts as part of the Header) at some point after you've starting sending the Body of the response i.e. your pages contents - in your case the message.
To solve this you can try setting Response.Buffer to true and then if you have to set a 404/500 response code then call Response.Clear() before setting the response code.
Note that if you are sending a 404/500 then there should generally be no body to the response (although the HTTP spec does allow for it)

What are some of the strategy to handle unhandled exception in asp.net mvc applications?

I would like to know some of the strategy/practice you deal with to handle unhandled exceptions in ASP.NET MVC.
In short I want to avoid the yellow screen whenever any error happens and show a error consistent error message to the visitor.
I mean do you write a controller for this which shows the appropriate error page or you go some other way like writing an httpmodule and trapping the error at a global level.
Any inputs in this direction is appreciated.
Using the HandleError attribute is the way to go. Here is a small sample which I use to handle Ajax calls from JQuery, ExtJs, and others.
On your controller
public class DataController : Controller
{
[HandleError(ExceptionType = typeof(ArgumentException), View = "ErrorAjax")]
public void Foo(string x, string y)
{
if (String.IsNullorEmpty(x))
throw new ArgumentException("String cannot be empty!");
// Call your layers or whatever here
AnotherCall();
}
}
Then on your view (ErrorAjax). Notice it's strongly typed (HandleErrorInfo)
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<HandleErrorInfo>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Sorry Dude!</title>
</head>
<body>
<div>
<!-- be creative here-->
Sorry, an error occurred while processing your request.
Action = <%= ViewData.Model.ActionName %>
Controller = <%= ViewData.Model.ControllerName %>
Message = <%= ViewData.Model.Exception.Message %>
</div>
</body>
</html>
A couple of gotchas
Check your web.config and make sure customErrors mode="On"
For starters, create the View under the Shared folder
Try the HandleError attribute.
Don't use the exception handling article that you linked to. It's an old article where they didn't have the HandleError attribute added in the framework. Use the HandleError attribute. It was added in preview 4.

Resources