简体   繁体   中英

Illegal characters in path. C# LoadControl

I am new to C# and working on a project with sitecore

i just added a aspx page with the

.aspx file :

 <sc:sublayout runat="server" renderingid="{7ACDFB04-E9C9-4AC4-BDC6-6F3FF0862199}" path="/layouts/FSB/Header.ascx" id="HeaderFixed"></sc:sublayout>
    <sc:placeholder runat="server" key="layout" id="templatePage"></sc:placeholder>
    <sc:sublayout runat="server" renderingid="{621A56F6-9948-4661-9F33-3AFEF1DE698D}" path="/layouts/FSB/Footer.ascx" id="FooterFixed"></sc:sublayout> 

.cs file

 Control templateControl = LoadControl("templateLandingPages/free_template.ascx");
 Control placeHolderControl = Page.FindControl("templatePage");

Error in browser shows for line no 16:

    Illegal characters in path.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.ArgumentException: Illegal characters in path.

Source Error: 

Line 14:    protected void Page_Load(object sender, EventArgs e)
Line 15:    {
Line 16:     Control templateControl = LoadControl("templateLandingPages\free_template.ascx");
Line 17:     Control placeHolderControl = Page.FindControl("templatePage");

Simple matter of how you use your \\ . You need to double up on the slash ( \\\\ ). The first one acts as an escape character for the second one.

Control templateControl = LoadControl("templateLandingPages\\free_template.ascx");

As commented, you can also use:

Control templateControl = LoadControl(@"templateLandingPages\free_template.ascx");

Check out this article on string literals and why you would use one notation over the other.

Put @ at starting of path string like @"templateLandingPages\\free_template.ascx"

CODE:

Control templateControl = LoadControl(@"templateLandingPages/free_template.ascx");

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.

C# supports two forms of string literals: regular string literals and verbatim string literals.

http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

You either need to use an escape or use a verbatim string.

var escaped = "templateLandingPages\\free_template.ascx";
var verbatim = @"templateLandingPages\free_template.ascx";

When using paths in C#, you need to use \\\\ rather than \\ . It escapes the backslash so that it will be just one literal \\ .

One way that I could solve the "illegal character in path" problem was by commenting all the member variable instantiations in the " Initialize() " function call, and then uncommenting each one of them one by one to find where the problem is exactly. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM