【问题标题】:The resource cannot be found when use global.asax使用 global.asax 时找不到资源
【发布时间】:2014-09-15 18:55:01
【问题描述】:

在网站面板中上传我的网站后,我收到此错误 找不到资源。

描述:HTTP 404。您正在寻找的资源(或其之一 依赖项)可能已被删除,名称已更改,或者是 暂时不可用。请查看以下 URL 并制作 确保拼写正确。

请求的 URL:/.aspx

我认为它与 global.asax 有关,因为当我将其从主机中删除时,我的网站会正常运行 我的 global.asax 代码

namespace officeWeb
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            String fullOrigionalpath = Request.Url.ToString();
            String[] sElements = fullOrigionalpath.Split('/');
            String[] sFilePath = sElements[sElements.Length - 1].Split('.');

            if (!fullOrigionalpath.Contains("__browserLink"))
            {
                            //Rewrite
            if (!fullOrigionalpath.Contains(".aspx") && sFilePath.Length == 1)
            {
                Context.RewritePath(sFilePath[0] + ".aspx");
            }
            }

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
        public static void RegisterRoutes(RouteCollection routes)
        {

            routes.Add("",
                new Route("root/pages/service/{*pathInfo}", new WebServiceRouteHandler("~/root/config/services.asmx")));

        }


        public class WebServiceRouteHandler : IRouteHandler
        {
            private static IHttpHandlerFactory ScriptHandlerFactory;
            static WebServiceRouteHandler()
            {
                var assembly = typeof(System.Web.Script.Services.ScriptMethodAttribute).Assembly;
                var type = assembly.GetType("System.Web.Script.Services.ScriptHandlerFactory");
                ScriptHandlerFactory = (IHttpHandlerFactory)Activator.CreateInstance(type, true);
            }

            private string virtualPath;
            public WebServiceRouteHandler(string virtualPath)
            {
                this.virtualPath = virtualPath;
            }

            public IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                string pathInfo = requestContext.RouteData.Values["pathInfo"] as 
                if (!string.IsNullOrWhiteSpace(pathInfo))
                    pathInfo = string.Format("/{0}", pathInfo);

                requestContext.HttpContext.RewritePath(this.virtualPath, pathInfo, requestContext.HttpContext.Request.QueryString.ToString());
                var handler = ScriptHandlerFactory.GetHandler(HttpContext.Current, requestContext.HttpContext.Request.HttpMethod, this.virtualPath, requestContext.HttpContext.Server.MapPath(this.virtualPath));
                return handler;
            }
        }
    }
}

我该怎么办? 请帮帮我

【问题讨论】:

    标签: c# asp.net routing global-asax


    【解决方案1】:

    如果您查看请求的 URL,它显示为 /.aspx。这是一个无效的 URL,因为它缺少实际的文件名并且只有扩展名。

    您需要调试代码并查看Application_BeginRequest() 中的变量值是否符合您的预期。可能不是。

    【讨论】:

    • 感谢您的回答,但是当我在 global.asax 中删除我的代码时,将空的 gloabl.asax 放入根目录中我得到同样的错误,在本地主机中它可以正常工作
    • 我很困惑。没有 global.asax 是否可以工作,或者没有它是否无法工作?在你的问题中,你说没有它就可以工作,现在你说没有它就不行。
    • 当我从主机中删除它时它可以工作,当我将它添加到主机时它的工作(我本地主机它的工作)
    猜你喜欢
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多