在尝试为Google网站站长工具设置我的网站后,我发现我的自定义ASP.NET 404页面未返回404状态代码.它显示了正确的自定义页面并告诉浏览器一切正常.这被认为是软404或假404.谷歌不喜欢这个.所以我发现了很多关于这个问题的文章,但我想要的解决方案似乎没有用.
我想要解决的解决方案是将以下两行添加到自定义404页面的Page_Load方法后面的代码中.
Response.Status = "404 Not Found";
Response.StatusCode = 404;
Run Code Online (Sandbox Code Playgroud)
这不起作用.页面仍然返回200 OK.然而,我发现如果我将以下代码硬编码到设计代码中,它将正常工作.
<asp:Content ID="ContentMain" ContentPlaceHolderID="ContentPlaceHolderMaster" runat="server">
<%
Response.Status = "404 Not Found";
Response.StatusCode = 404;
%>
... Much more code ...
</asp:content>
Run Code Online (Sandbox Code Playgroud)
该页面正在使用母版页.我正在我的web.config中配置自定义错误页面.我真的宁愿使用后面的代码选项但我似乎无法在设计/布局中放入黑客内联代码使其工作.
在旧网站中,我通过添加redirectMode="ResponseRewrite"(3.5 SP1中的新增功能)来更改CustomErrors的工作方式:
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="404.aspx" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
问题是:它向我显示了一般错误页面(当你没有设置时你得到的那个customErrors.如果我删除redirectMode="ResponseRewrite"部分,它工作正常.
我确定服务器中安装了3.5 SP1,因为我在同一服务器上托管的其他站点上使用相同的设置.
有任何想法吗?
我试图用div而不是标签来越过默认的错误消息标签.我也看过这篇文章,并了解如何做到这一点,但我对CSS的限制令我难以忘怀.我如何像其中一些示例一样显示:
示例#1(Dojo) - 必须键入无效输入才能看到错误显示
示例#2
下面是一些示例代码,它将错误标签覆盖为div元素
$(document).ready(function(){
$("#myForm").validate({
rules: {
"elem.1": {
required: true,
digits: true
},
"elem.2": {
required: true
}
},
errorElement: "div"
});
});
Run Code Online (Sandbox Code Playgroud)
现在我对css部分感到茫然,但现在它是:
div.error {
position:absolute;
margin-top:-21px;
margin-left:150px;
border:2px solid #C0C097;
background-color:#fff;
color:white;
padding:3px;
text-align:left;
z-index:1;
color:#333333;
font:100% arial,helvetica,clean,sans-serif;
font-size:15px;
font-weight:bold;
}
Run Code Online (Sandbox Code Playgroud)
更新:
好吧我现在正在使用这个代码,但是弹出窗口上的图像和位置比边框大,可以调整为动态高度吗?
if (element.attr('type') == 'radio' || element.attr('type') == 'checkbox') {
element = element.parent();
offset = element.offset();
error.insertBefore(element)
error.addClass('message'); // add a class to the wrapper
error.css('position', 'absolute');
error.css('left', offset.left + …Run Code Online (Sandbox Code Playgroud) 我在C#中创建了一个自定义属性,我希望根据属性是应用于方法还是属性来执行不同的操作.起初我打算new StackTrace().GetFrame(1).GetMethod()在我的自定义属性构造函数中查看调用属性构造函数的方法,但现在我不确定这会给我什么.如果属性应用于属性怎么办?会GetMethod()返回MethodBase该属性的实例吗?是否有不同的方法来获取在C#中应用属性的成员?
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property,
AllowMultiple = true)]
public class MyCustomAttribute : Attribute
Run Code Online (Sandbox Code Playgroud)
更新: 好吧,我可能一直在问错误的问题.在自定义属性类中,如何获取应用了自定义属性的成员(或包含成员的类)? Aaronaught建议不要走向堆栈以找到应用了我的属性的类成员,但是如何从属性的构造函数中获取此信息呢?
我喜欢提供有用的错误/消息,我也想为我static_assert的.问题是,它们依赖于模板参数.通常情况下,由于引发的错误,这些参数会在途中或其他参数上显示,但它们要么模糊不清,要么不分组,因此它们有意义.例:
template<class T>
struct fake_dependency{
static bool const value = false;
};
template<class T, class Tag>
struct Foo{
Foo(){}
template<class OtherTag>
Foo(Foo<T, OtherTag> const&){
static_assert(fake_dependency<T>::value, "Cannot create Foo<T,Tag> from Foo<T,OtherTag>.");
}
};
int main(){
Foo<int, struct TagA> fA;
Foo<int, struct TagB> fB(fA);
}
Run Code Online (Sandbox Code Playgroud)
MSVC上的输出:
src\main.cpp(74): error C2338: Cannot create Foo<T,Tag> from Foo<T,OtherTag>.
src\main.cpp(84) : see reference to function template instantiation 'Foo<T,Tag>::Foo<main::TagA>(const Foo<T,main::TagA> &)' being compiled
with
[
T=int,
Tag=main::TagB
]
Run Code Online (Sandbox Code Playgroud)
函数模板本身提到了一个标记,下面是类模板.不太好.让我们看看海湾合作委员会的成果:
prog.cpp: In constructor 'Foo<T, Tag>::Foo(const …Run Code Online (Sandbox Code Playgroud) 我想将自定义错误设置为true,以防止用户查看有关我的应用程序的详细信息.但我找不到我应该写的地方<customErrors mode="on">; 它应该在web.config中还是在web中,debug.config或者在哪里?
BR
虽然这更像是一种书面语言问题而不是编码语言问题,但程序员必须在客户或其他人不提供复制的情况下这样做.任何错误消息的例子,无论好坏,都欢迎指出.
我简短地搜索过,找不到欺骗线程.好的,有它.谢谢,所有.
那些不需要这些工具的概念不是C++ 11的一部分.
有哪些生产质量工具可用于解析源自基于模板的代码的错误消息?Eclipse-CDT支持也很不错.:)
如果我放弃C++ 11,我对C++ 98有什么选择?
相关问题:
当我尝试使用远程连接到我的服务器应用程序时出现以下错误:
连接到远程服务器时似乎出现了问题:
服务器遇到内部错误.有关更多信息,请关闭服务器.config文件中的customErrors.
这是我的服务器应用程序上的代码:
TcpChannel tcpChannel = new TcpChannel(999);
MyRemoteObject remObj = new MyRemoteObject (this);
RemotingServices.Marshal(remObj, "MyUri");
ChannelServices.RegisterChannel(tcpChannel);
Run Code Online (Sandbox Code Playgroud)
它似乎第一次工作,但除非重新启动服务器应用程序,否则会发生错误.
我猜想有些东西没有被正确清理,但我不确定是什么因为customError仍然存在.
我开始的任何想法.谢谢.
[编辑] - 感谢Gulzar,我将上面的代码修改为以下内容,现在显示错误:
RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
TcpChannel tcpChannel = new TcpChannel(999);
MyRemoteObject remObj = new MyRemoteObject (this);
RemotingServices.Marshal(remObj, "MyUri");
ChannelServices.RegisterChannel(tcpChannel);
Run Code Online (Sandbox Code Playgroud) 我在ASP.NET MVC 5应用程序中使用自定义authorize属性,如下所示:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext context)
{
if (context.HttpContext.Request.IsAuthenticated)
{
context.Result = new System.Web.Mvc.HttpStatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);
}
else
{
base.HandleUnauthorizedRequest(context);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在system.web我的web.config部分,我提到了错误路径,如:
<system.web>
<customErrors mode="On" defaultRedirect="/Error/Error">
<error statusCode="403" redirect="/Error/NoPermissions"/>
</customErrors>
</system.web>
Run Code Online (Sandbox Code Playgroud)
但我从未被重定向到我的自定义错误页面/Error/NoPermissions.相反,浏览器显示"HTTP错误403.0 - 禁止"的常规错误页面.
custom-errors ×10
c# ×3
asp.net ×2
c++ ×2
c++11 ×2
templates ×2
.net ×1
asp.net-mvc ×1
attributes ×1
css ×1
iis-6 ×1
jquery ×1
master-pages ×1
popup ×1
redirectmode ×1
reflection ×1
remoting ×1
stl ×1
tooltip ×1
usability ×1
web-config ×1