简体   繁体   English

如何在web.config文件中获取相对路径

[英]How to get a relative path in a web.config file

I searched around stackoverflow on how to make a relative file and have tried various things and it hasn't worked, and i was hoping to see if you guys could help me out. 我在stackoverflow上搜索了如何制作相对文件的方法,并尝试了各种方法,但没有成功,我希望看看你们是否能帮助我。

here is my connection sting in my web.config file: 这是我的web.config文件中的连接字符串:

 <add name="2007 Database  05-12-2013(Esfahanian's conflicted copy 2013-06-24)
  ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data 
  Source=" providerName="System.Data.OleDb"/>

and here relative path in my aspx file: 这是我的aspx文件中的相对路径:

<script runat="server">
    string connectionString = ConfigurationManager
    .ConnectionStrings["2007 Database 
    05-12-2013(Esfahanian's conflicted copy 2013-06-24) ConnectionString"]
   .ConnectionString + Server.MapPath("..\..\Anderson\2007 
    Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb");
</script>

And I get this error: CS1009: Unrecognized escape sequence 我收到此错误: CS1009: Unrecognized escape sequence

So what exactly am I doing wrong 那我到底在做什么错

You aren't escaping the "\\" character in your path so it is causing an error in the MapPath() method. 您没有在路径中转义“ \\”字符,因此它在MapPath()方法中导致错误。

Change this: 更改此:

Server.MapPath("..\\..\\Anderson\\2007 Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb"

to this: 对此:

Server.MapPath(@"..\\..\\Anderson\\2007 Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb"

or this: 或这个:

Server.MapPath("..\\\\..\\\\Anderson\\\\2007 Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb"

You are using \\ in your path string but thats the symbol for literal. 您在路径字符串中使用\\,但这就是文字的符号。 So any char following the \\ will be inturpreted as a literal like \\. 因此,\\之后的任何char都会被解释为\\之类的文字。 is not a valid char. 不是有效的字符。 You actualy want the literal \\ the sequence for which is \\\\ Also @ infront of a string tells that you dont want literals. 实际上,您实际上希望文字\\的顺序是\\\\。另外,字符串的@开头表示您不需要文字。

So mappath(" needs to be mappath(@" or every \\ needs to become \\\\ 因此mappath(“必须是mappath(@”,否则每个\\都必须成为\\\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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