简体   繁体   English

.aspx文件中的可选参数

[英]Optional Parameters in .aspx file

I have a static method (in the class CommonHelper in the namespace Common) that I want to use for including static resources that has a definition like the following: 我有一个静态方法(在名称空间Common中的CommonHelper类中),我想用它来包含具有如下定义的静态资源:

public static string GetUrlForStaticResource(
String fileName, String folder="scripts", bool preventCaching=false)

You can see the "folder" and "preventCaching" parameters are optional as they have default values. 您可以看到“folder”和“preventCaching”参数是可选的,因为它们具有默认值。 When I call the method from a .cs file it works fine but in my .aspx I tried the following and got an error CS1501: No overload for method 'GetUrlForStaticResource' takes '1' arguments : 当我从.cs文件调用该方法时,它工作正常但在我的.aspx中我尝试了以下操作并得到错误CS1501:方法'GetUrlForStaticResource'没有重载需要'1'参数

<div>Example: <%= Common.CommonHelper.GetUrlForStaticResource("test.js") %></div>

If I provide all the parameters it works fine: 如果我提供所有参数,它可以正常工作:

<div>Example: <%= Common.CommonHelper.GetUrlForStaticResource("test.js", "scripts", false) %>
</div>

What am I doing wrong? 我究竟做错了什么? Are optional parameters not available in .aspx? .aspx中没有可选参数吗?

I tested this and it works fine for me. 我测试了这个,它对我来说很好。 I used .NET 4.0 and CommonHelper as a static class (with no explicit constructor). 我使用.NET 4.0和CommonHelper作为静态类(没有显式构造函数)。

I then changed the web project to .NET 2.0. 然后我将Web项目更改为.NET 2.0。 The project builds, and then I get the same error you report at runtime. 项目构建,然后我得到您在运行时报告的相同错误。

当然,您也可以为GetUrlForStaticResource创建一个只接受一个参数的重载,然后使用默认值从那里调用主方法。

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

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