Using HttpModule with .html file extension - c#

I need access to session in httpmodule. It works fine when my page is a aspx page, but context.session is null when the request url is .html
I have .html mapped to use aspnet_isapi.dll
I am trying to access session in context_PreRequestHandlerExecute and I have httpmodule inherit IReadOnlySessionState

From my experience IReadOnlySessionState and IRequiresSessionState only apply to HttpHandlers.
See the following SO links on how to implement it:
Can I access session state from an HTTPModule?
IIS HttpModule unable to set Session

It does not work with HTML extension because aspnet_isapi.dll does not handle the extension, but handels .aspx pages.
You should use another extension instead of using HTML and you should register the new extension in IIS Application Configuration (Web Site Properties -> Home Directory tab -> Configuration button -> Mappings tab). Use .aspx as an example to add your own extension.

Related

To Modify the .htaccess for 301 Redirect with Asp.net on the fly (Dynamically)

I make changes using Cpanel / Redirect manager of a Site.
Based on the Changes made on Cpanel for URL redirection, i.e. Say Old Url -> Current Url, I want to modify the .htaccess file dynamically. How do I achieve this using Asp.net?
I assume you want to do URL redirection in ASP.NET. See more information about this topic. http://www.iis.net/downloads/microsoft/url-rewrite.

URL Rewriting Not working using Global.asax

I am trying to do URL Rewriting from Global.ascx file but every time it redirects to Default.aspx page and ignores the Tab id as i am doing it in DotNetNuke ver. 06.01.03 (108). Here is the code:
if (CurrentURL_Path.Contains("scientific-cameras"))
{
HttpContext.Current.RewritePath("~/Default.aspx?TabId=105");
}
I have created a page abc.aspx
I want that if "scientific-cameras" is contains in the url then it redirect to the page (abc.aspx) and 105 is TabId for abc.aspx.
Use Routed Table URL Routing with ASP.NET 4.0
http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET-4-0
More than likely this is a conflict with the DNN stuff. There is already a friendly URL structure in DNN. I would recommend using this rather than doing your own, as you don't want too many cooks in the kitchen.
You can specify custom URL's in "Host" -> "Host Settings" -> "Friendly URL Settings".

access header of page in httpmodule

Using a httpmodule I want to add a meta tag to all pages in my web application at run time. So I need to access the header section of my page. How can I do this in a httpmodule?
You would be better served by inserting this META tag into the header of your site's Master Page (and creating and using a Master Page if you don't already have one.) That give's you a central location for it, while not having the overhead of a module in the pipeline.
If you need to use an HttpModule, see this link about installing a response filter.
You probably want the HttpApplication and the current context of that so you can modify the request and response streams.

Getting text after URL in asp.net / URL Rewriting (sort of!)

My app is a very simple "one page" type app-
It has Default.aspx
I'm basically trying to get, for example:
www.myappurl.com/this is my text
I want to get hold of "this is my text" from the above example.
This will be displayed on the page (for now)
I didn't really want to have to use any complext url rewriting things for this...
(My hosting provider uses IIS6)
I tried using a 404 handler, but this is a bit long winded, and i'm using shared hosting, that can't set the "execute url" on custom 404 pages.
Any other ideas?
You can add a mapping for all requests with the * extension to the ASP.NET isapi dll (GET/POST) verbs. You will need to uncheck the "verify file is on disk" checkbox when mapping the extension in IIS. (In IIS7 integrated mode, you map the extension in the web.config as well). Note that this will caause everything to be served by asp.net, even images and script files, which can slow things down.
Then create a handler mapping in your web.config to a http handler you create.
From there, in the ProcessRequest() method of the handler, you have access to the HttpContext that spawned the request and can manipulate the URL from there.
That is the easiest option, you could also create a HttpModule, or have the default page at root redirect to http://www.domain.com/default.aspx/this is my text, in the code-behind of default.aspx, you will be able to get the text following the page and slash.

ASP.NET/SharePoint master page issue

I am using SharePoint Server 2007 + C# + .Net 3.5 + VSTS 2008 + ASP.Net. And I am using collaboration portal template.
I am developing a custom aspx page and put it in _layout folder of a site and I want to apply default.master of the SharePoint site to this aspx page. Any samples about how to achieve this goal?
You can put code in the PreInit event to make your custom page use the current site's masterpage.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
SPWeb myWeb = SPControl.GetContextSite(Context).OpenWeb();
string strUrl = myWeb.ServerRelativeUrl + "/_catalogs/masterpage/my.master";
this.MasterPageFile = strUrl;
}
Or replace my.master with default.master in order to use the default master page. Or check spweb's MasterUrl property and use that instead.
Either way it should get you going in the right direction.
If you use SharePoint Designer then you can right-click on your page and then select a Master page to apply.
The best way to do this is by use of an HttpModule. This enables the use of your custom master page for all application pages (i.e. pages in the LAYOUTS folder). It can be deployed using a feature and can be activated per web application (seeing as the httpmodule needs to be registered in the web.config it is web app scoped.)
By making it web app scoped, your end users will have a uniform user experience, instead of that single page looking like the front end of the site while all the other (Out of the box) application pages are still using the default sharepoint application.master.
For a code example and a more in depth explanation, look here.
P.S. You are getting errors using the code above because of missing content placeholders. You need to create a copy of your custom master page. Although styling can be the same, application pages use more/other ContentPlaceHolders than a front end master page.
Just copy your custom master page, rename it from CustomMaster.master to, say, CustomMasterEdit.master and use that for application page styling, SharePoint will throw an error telling you which placeholders are missing, keep adding the needed placeholders till the page works (there are 2 or 3 extra placeholders needed i believe).
P.P.S. To make sharepoint display errors that make sense, go the web.config and look for the <SharePoint> tag and change the callstack attribute from false to true. Then, look for the customErrors tag and set the mode attribute to "off". To completely enable debugging, you can also enable ASP.NET tracing. Of course you should NOT do this in your production environment....
More info on modifying the web.config to make sharepoint display real error message can be found here.
and

Categories

Resources