How to insert code in the static files like .html, .htm, .asp on IIS for non-asp project - httphandler

I want to add a script on my IIS Server.
So that it will be applied on all the websites that are upload will have that script in their request response.
Anyone who knows how to do it?
I had implemented the IHttpModule and IHttpHandler, it works fine for the asp.net projects.
but if the website contains only html, css, and js files in the folder, this solution doesn't work.
Here the HttpModule and HttpHandler
public class MyCustomHttpModuleClass : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += OnPostRequestHandlerExecute;
}
public void OnPostRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
context.Response.Write("<h1>alert('HELLO')</h1>");
}
}
public class MyHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Write("<h1>alert('HELLO')</h1>");
}
}

I'm not sure if you have learnt how to add Custom Module and Handler in IIS. After tested your module and handler with static website, it works fine.
I will just give you a sample of adding them to IIS.
1.Create a project "class library .netframework". I name the project"ClassLibrary1"
2.Add class "MyCustomHttpModuleClass" and "MyHandler" to the project
3.Build this solution and find "ClassLibrary1.dll" in the "project/bin/debug" folder.
4.Copy "ClassLibrary1.dll" to the website root "BIN" folder.
5.Add managed module and handler by choose your dll.(should in the list after you copied)Just mention that your custom handler only work on the file extension you set up.
Now they work.

Related

Install a plugin to Liferay's editor

I have a Liferay DXP installation and I would like to install a plugin to the editor. The plugin is base64image.
I was following this official guide so I created a class generally like this:
#Component(immediate = true, service = DynamicInclude.class)
public class CKEditorBase64ImageDynamicInclude implements DynamicInclude {
private BundleContext bundleContext;
#Override
public void include(HttpServletRequest request, HttpServletResponse response, String key) throws IOException {
Bundle bundle = bundleContext.getBundle();
URL entryURL = bundle.getEntry("/META-INF/resources/html/editors/ckeditor/extension/base64_images.js");
StreamUtil.transfer(entryURL.openStream(), response.getOutputStream());
}
#Override
public void register(DynamicIncludeRegistry dynamicIncludeRegistry) {
dynamicIncludeRegistry.register("com.liferay.frontend.editors.web#ckeditor#onEditorCreate");
}
#Activate
protected void activate(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
}
It should include the base64_images.js file where it initializes the editor. But it never works, regardless what the content of the file is. What is wrong with that?
I would like to add that the plugin files (JavaScript code) are part of my Liferay theme. I wanted base64_images.js to call its API but it also might not be the correct way how to do it.

Impossible to show Custom Error page with Nancy on OWIN

I have a website using Nancy which is hosted using OWIN.
In my Startup.cs file I define the PassThroughOptions as follows:
public void Configuration(IAppBuilder app)
{
app.UseNancy(o => {
o.PassThroughWhenStatusCodesAre(
HttpStatusCode.NotFound,
HttpStatusCode.InternalServerError
);
o.Bootstrapper = new Bootstrapper();
});
app.UseStageMarker(PipelineStage.MapHandler);
}
I need to pass-through the NotFound requests, so that things like my bundled .less files or miniprofiler-results or static files in the root of my site (robots.txt or sitemap.xml) work.
I also have a custom StatusCodeHandler for the 404 code, which also checks a custom header to distinguish between static files (or .less bundles/miniprofiler) and actual stuff that is not found in my modules' methods.
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
Log.Warn("Not found: " + context.Request.Url);
base.Handle(statusCode, context, "Errors/NotFound");
}
This handler then should actually show the error page.
protected void Handle(HttpStatusCode statusCode, NancyContext context, string view)
{
var response = new Negotiator(context)
.WithModel(GetErrorModel(context))
.WithStatusCode(statusCode)
.WithView(view);
context.Response = responseNegotiator.NegotiateResponse(response, context);
}
But the error page is never shown. The request is processed three times and eventually the default IIS error page is shown (using errorMode="Custom" for httpErrors) or simply a white page (using existingResponse="PassThrough" for httpErrors).
Is there any way to display something so simple as a custom error page when hosting a Nancy website on OWIN?
What you've got there looks good, it looks like you've be using the Hosting Nancy with Owin docs.
Here's what works for me:
The Startup.cs (required for Owin): (We've both coded the configuration function differently, you're just using the extension helper while I'm not. Same result. This is in my App.Web project.)
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseNancy(options =>
{
options.Bootstrapper = new BootStrapper();
options.PerformPassThrough = context => context.Response.StatusCode == HttpStatusCode.NotFound;
});
app.UseStageMarker(PipelineStage.MapHandler);
}
}
404 handler: (As per the docs, doesn't matter where this is in the project, by implementing IStatusCodeHandler it'll be automatically picked up by Nancy This is in my App.WebApi project with other module classes.)
public class StatusCode404Handler : IStatusCodeHandler
{
public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context)
{
return statusCode == HttpStatusCode.NotFound;
}
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
var response = new GenericFileResponse("statuspages/404.html", "text/html")
{
StatusCode = statusCode
};
context.Response = response;
}
}
The 'statuspages' folder in my App.Web project:
Check this SO post for a comparison of using GenericFileReponse or ViewRenderer (How to display my 404 page in Nancy?).

ASP.NET MVC (Content-language http header)

I'm developing content site (internationalised) using ASP.NET MVC. I use web.config (not clientbrowser setttings) to deliver region specific content.
<globalization culture="fr" uiCulture="fr" enableClientBasedCulture="false" />
I don't see ASP.net MVC framework is appending "Content-language" header automatically, is there a way to do that, and if yes than how. And if now than how can we put customised code most efficiently.
Regards.
In your controller, add:
Response.AddHeader("Content-language",
Thread.CurrentThread.CurrentUICulture.Name);
you can apply it to all actions by creating a base class for all your controllers, and including this in overridden OnActionExecuting. Such as:
public class MyController : BaseController
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
Response.AddHeader("Content-language",
Thread.CurrentThread.CurrentUICulture.Name);
base.OnActionExecuting(filterContext);
}
}
Your controllers should then be changed to use MyController instead of BaseController as their base class.
you can apply it to all actions by add this code to global.asax.cs
protected void Application_BeginRequest(object sender, EventArgs e)
{
Response.AddHeader("Content-language", Thread.CurrentThread.CurrentUICulture.Name);
}

Determining application context from OWIN pipeline?

I have an OWIN pipeline using Nancy:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseNancy();
}
}
The UseNancy() is actually a call to my own custom extension method defined in this gist: https://gist.github.com/TheFastCat/0b7635d9e5795b44e72e
This code is executed both as an Azure Website or an Azure Cloud Service. Based on the context it is executing within I want to use a particular favicon, loaded as an embedded resource from a separate assembly. I do this by specifying separate NancyBootstrappers (each loading the proper favicon for its context).
Is there a more elegant solution to determining the runtime application that is executing the OWIN pipeline? Currently I check app.Properties["host.AppName"] ; however while the Website's app name matches it's assembly configuration, the CloudService app is the name of the Owin startup assembly.class. (see gist). It's cloogey.
Is there a more elegant/simple solution for specifying a custom favicon within Nancy for each of my web applications than creating separate bootstrappers and doing runtime application context checks?
I solved this problem with the help of others on the https://jabbr.net/#/rooms/owin and https://jabbr.net/#/rooms/nancyfx chat boards
Yes. You can contextually check the OWIN host properties:
if (app.Properties.ContainsKey("System.Net.HttpListener"))
{
// self hosted application context
}
2.) Yes.
namespace ClassLib
{
public class Startup()
{
public Startup(byte[] favIcon) { ... }
public void Configuration(IAppBuilder app) { ... }
}
}
[assembly: OwinStartup(typeof(WebHost.Startup))]
namespace WebHost
{
public class Startup()
{
public voic Configuration(IAppBuilder app)
{
new ClassLib.Startup(webhostFavIcon).Configuration(app);
}
}
}
namespace SelfHost
{
private class Program()
{
public void Main(string[] args)
{
using(WebApp.Start(app => new ClassLib.Startup(selfHostFavIcon).Configuration(app))
{}
}
}
}

Displaying an html page in form (error connecting to stream)

I putted an .html page in a src folder of project in order to display this page on runtime.
But I get an error on runtime that say:- Error connecting to stream.
import javax.microedition.midlet.*;
public class HtmlMidlet extends MIDlet {
public void startApp()
{
com.sun.lwuit.Display.init(this);
final com.sun.lwuit.Form form = new com.sun.lwuit.Form("");
final com.sun.lwuit.html.HTMLComponent htmlC = new com.sun.lwuit.html.HTMLComponent( );
htmlC.setRTL(true);
htmlC.setPage("jar://src/ahlam.html");
form.addComponent(htmlC);
form.setScrollable(true);
form.show( );
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
}
I think that the way that you are using to open your file is not the proper way. Is this html page in your folder? Where is your page? the root directory is /src from your project, why are you using jar:... try /ahlam.html only
Take a look on this page for more info and examples HTML Component

Resources