I created an HttpModule to restrict access from certain IPs. I want to use as little bandwidth as possible and not return the default IIS 404 page. I would rather return no content at all.
private static void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
app.Response.StatusCode = 404;
app.Context.Response.Clear();
app.Context.Response.End();
}
Changing the status code to 200 works and create 0 bytes of content in the response but now I'm curious as to how to do it no matter the statuscode.
<customErrors mode="On" defaultRedirect="~/Error/Index.html">
<error statusCode="404" redirect="~/Error/404Error.html" />
</customErrors>
Add this to your web.config. And of course, adjust urls based on your project.
I need to redirect www.domain.com/.well-known/pki-validation/godaddy.html from my webpage to get SSL Certificate from godaddy. but when im given the file path from the web.config file to redirect to it suddenly my site not working. the reason is dot sign.
Please can you guys help me to figure this out.
Thanks
Give proper url with http:\\
protected void Page_Load(object sender, EventArgs e)
{
string url= WebConfigurationManager.AppSettings["url"].ToString();
Response.Redirect(url);
}
<appSettings>
<add key="url" value="your url""/>
<add key="SiteName" value="New York Times"/>
</appSettings>
I have a website developed in Classic ASP.NET and running on IIS 7.5. It works okay. But now I have the task to add affiliate program to my website. I want my reflink to looks like www.mywebsite.com/r/userid. Well, I googled around and found I can:
Use UrlRewrite third-party HttpModules. As I understand, they are based on runAllManagedModulesForAllRequests="true" web.config setting. Theoretically, I can:
Set runAllManagedModulesForAllRequests="true" and do RewritePath in Application_BeginRequest manually. But my Application_BeginRequest already contains a bit of code. It is too heavy to send all pages, images etc. to Application_BeginRequest because of one rarely called URL.
So, the question is how can I rewrite only subpath that starts with www.mywebsite.com/r/, and do not call Application_BeginRequest for every image, css etc.? Best if no third-party things.
Well, finished up with HttpModule.
web.config:
<system.webServer>
<modules>
<add name="ReflinkModule" preCondition="" type="www.ReflinkModule" />
</modules>
</system.webServer>
ReflinkModule.cs:
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginRequest);
}
public void BeginRequest(Object source, EventArgs e)
{
if (HttpContext.Current == null)
return;
if (HttpContext.Current.Request == null)
return;
if (HttpContext.Current.Request.RawUrl == null)
return;
string start = "/r/";
if (!HttpContext.Current.Request.RawUrl.StartsWith(start))
return;
string key = HttpContext.Current.Request.RawUrl.Substring(start.Length);
HttpContext.Current.RewritePath("~/Xxxxx.aspx?r=" + key);
}
It is necessary to register as soon as this user directly login
protected void Button1_Click(object sender, EventArgs e)
{
Membership.CreateUser(FNBox.Text, PassBox.Text, EmailBox.Text);
FormsAuthentication.RedirectToLoginPage(FNBox.Text);
}
Error:
"Could not find the resource.
Description: HTTP 404. Perhaps the desired resource (or one of its dependencies of components) is removed, has a different name or is temporarily unavailable. Look at the following URL-address and make sure it is correct.
The requested URL: / OrderTest2/login.aspx "
protected void LoginButton_Click(object sender, EventArgs e)
{
Control lgnview = (Control)LoginView2.FindControl("LoginForm");
TextBox usrbox = (TextBox)lgnview.FindControl("UserName");
TextBox pasbox = (TextBox)lgnview.FindControl("Password");
string user = usrbox.Text;
string pass = pasbox.Text;
if(Membership.ValidateUser(user,pass))
{
FormsAuthentication.RedirectToLoginPage(user);
}
}
This is work normally
Your default redirect login page is wrong. As you can see, you are getting a 404 error which means that IIS cannot find your OrderTest2/login.aspx page. Verify your path.
You can set the defaultUrl path in your web.config as below to a valid page path, this will fix the problem.
<authentication mode="Forms">
<forms loginUrl="/OrderTest2/login.aspx" defaultUrl="myCustomLogin.aspx" cookieless="UseCookies" />
</authentication>
I'm having some trouble getting log4net to work from ASP.NET 3.5. This is the first time I've tried to use log4net, I feel like I'm missing a piece of the puzzle.
My project references the log4net assembly, and as far as I can tell, it is being deployed successfully on my server.
My web.config contains the following:
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler
, log4net"
requirePermission="false"/>
</configSections>
<log4net>
<appender name="InfoAppender" type="log4net.Appender.FileAppender">
<file value="..\..\logs\\InfoLog.html" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<logger name="_Default">
<level value="INFO" />
<appender-ref ref="InfoAppender" />
</logger>
</log4net>
I'm using the following code to test the logger:
using log4net;
using log4net.Config;
public partial class _Default : System.Web.UI.Page
{
private static readonly ILog log = LogManager.GetLogger("_Default");
protected void Page_Load(object sender, EventArgs e)
{
log.Info("Hello logging world!");
}
}
In my Global.asax, I'm doing the following:
void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
At this point, I can't think of what else I might be doing wrong. The directory I'm trying to store the log in is writable, and even if I try different directories I get the same result: no file, no logs.
Any suggestions? :-)
Edit: I've tried several different formats for the path & name of the log file, some of which include "..\..\InfoLog.html", "InfoLog.html", "logs\InfoLog.html", etc, just in case someone is wondering if that's the problem.
Edit: I've added the root logger node back into the log4net section, I ommitted that on accident when copying from the samples. The root logger node looks like this:
<root>
<level value="INFO" />
<appender-ref ref="InfoAppender" />
</root>
Even with it, however, I'm still having no luck.
The root logger is mandatory I think. I suspect configuration is failing because the root doesn't exist.
Another potential problem is that Configure isn't being pointed to the Web.config.
Try Configure(Server.MapPath("~/web.config")) instead.
It sounds very much like a file permissions issue to me. If you specify a file name without any path, log4net will write to the root directory of the web application. Try that. Barring any success there, I'd recommend you enable internal log4net debugging by putting the following in your web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
Then, deploy the app compiled in debug mode and run the visual studio remote debugger to see any errors that are thrown.
Iv just spent 3 hours trying to fix this problem is the end it was the web.config formatting.
I had this, and it didn't work:
<section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" requirePermission="false"/>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler
, log4net"
requirePermission="false"/>
changing it to this fixed it:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" requirePermission="false"/>
Annoying!!
Just for info: the file path must be formatted as follows:
<file value="..\\..\\logs\\InfoLog.html" />
A simple configuration tutorial from CodeProject
I don't have the Global.asx to run in .net 3.5
this is my code... I configure the log4net in a separate file log4net.config
// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
//My Web Services class name
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("Service1");
i had the same (frustrating) problem. i moved the root to the top of the log4net config section and it all worked. Also, i too had root and a logger element, this resulted in two lines each time i made a call to the logger. i removed the logger element and simply keep the root and its working great.
How about creating the logger with the page's type, like this:
private static readonly ILog log = LogManager.GetLogger(typeof(_Default));
This is what i have in Global.ASX. Got it all working in asp.net 3.5
<%# Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
log4net.Config.XmlConfigurator.Configure();
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
log4net.LogManager.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>