且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的代码不起作用?

更新时间:2023-02-03 19:26:37

Regex.Split需要2个字符串,一个输入和一个模式。您正在尝试使用Regex.Split,就像它的String.Split一样。基本上你的正则表达式是无效的,可能抛出异常,然后下面的部分没有运行。



而不是使用Regex.Split,使用String.Split,如:



  string  url =   sfsdfs / sdfsfs / sdfsfs / sdfsf /; 
string [] splits = url.Split(' / 跨度>);
应用程序[ num] = splits.Length.ToString();
string Url = Page.GetRouteUrl( ITEM new {NAME = MOHAMMAD});
Response.Redirect(url);


hello

why this cod is not working?

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {

        this.routingData(RouteTable.Routes);

    }

    void routingData(RouteCollection routes)
    {

        int num = Convert.ToInt32 (HttpContext.Current.Application["num"]);
        string name = string.Empty;
        for (int i = 0; i < num; i++)
        {
            name += "{Name" + i + "}" ;
        }

        routes.MapPageRoute("ITEMs", name , "~/Default.aspx");

        routes.MapPageRoute("ITEMsssss", "cat/{NAME}" + name, "~/ITEM.aspx");

        
                
        
        
        
    }
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application 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>



[Edit]

my behind code is : 

 protected void Button1_Click(object sender, EventArgs e)
    {
        //Application["numCat"] = 2;
        //object category = "mohammad/reza";
        //Page.RouteData.Values["id"] = 1;
        //Response.Redirect("category/"+category + "/item/" + 1 + "-ALIAsS");
        string url = "sfsdfs/sdfsfs/sdfsfs/sdfsf/";
        string[] splits  = Regex.Split(url,"/");
        Application["num"] = splits.Length.ToString();
        string Url = Page.GetRouteUrl("ITEMs", new { NAME = "MOHAMMAD" });
        Response.Redirect(url);

    }



when i click on my button should split my URL and get number . then set application["num"] = number of split;

then in global URL routing map create .

don't create a name in my global for URL routing

[/Edit]

Regex.Split takes 2 strings, an input and a pattern. You are trying to use Regex.Split like its String.Split. Basically your Regex is invalid, probably throwing an exception, and then the parts underneath are not being run.

Instead of using Regex.Split, use String.Split, like:

string url = "sfsdfs/sdfsfs/sdfsfs/sdfsf/";
string[] splits  = url.Split('/');
Application["num"] = splits.Length.ToString();
string Url = Page.GetRouteUrl("ITEMs", new { NAME = "MOHAMMAD" });
Response.Redirect(url);