我已经在 Visual Studio 2012 中创建了第一个 ASP Web 服务,其代码现在如下所示(这是文件 TestService.asmx.cs
):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace MyWS
{
[WebService(Namespace = "http://something.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WS1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
我现在想做的是将类 WS1
重命名为更具信息性的名称,例如 HelloWorldService
。为此,我认为 Visual Studio 的重构功能应该足够了,因此我选择了 WS1,单击重构并重命名了该类。但是,当运行 Web 服务时,尝试导致服务器错误:
Parser Error Message: Could not create type 'MyWS.WS1'.
从中我得出结论,我的项目中仍有一部分认为 Web 服务名为 WS1
。我发现文件 TestService.asmx
保持不变,仍然包含行
<%@ WebService Language="C#" CodeBehind="TestService.asmx.cs" Class="MyWS.WS1" %>
我的问题如下:
有没有办法通过VS的重构方法来重命名.asmx
文件中的webservice?如果不是,避免此类问题的最佳方法是什么?
谢谢。
最佳答案
VS 重构功能通常仅限于代码名称(即变量名称、函数名称、类名称),但 Class="中的
被 VS 视为字符串值,可以是任何内容。 ReSharper 插件有自己的重构模块,可以搜索字符串引用,但同样,您需要手动验证更改是否正确并且应该应用。 MyWS.WS1
MyWS.WS1"
手动重命名服务绝对没问题,只需在解决方案范围内进行常规文本搜索 WS1
即可确保您没有遗漏任何内容。
我测试了一下,它似乎只在代码文件中搜索.net string
类型的字符串。所以如果你有
var name = "WS1";
它将包含在“字符串”搜索结果中。
关于c# - 在 Visual Studio 中重构 .asmx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25074361/