1

我在 _Layout.cshtml 中有一个 ajax 调用。

<script lang="javascript" type="text/javascript">
    function ttsFunction() {
        serviceUrl = "http://localhost:8080/wscccService.svc/RunTts";
        var data = new Object();
        data.text = $('#speak').val();
        var jsonString = JSON.stringify(data);
        $.ajax({
            type: 'POST',
            url: serviceUrl,
            data: jsonString,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',

            error: function (xhr,status,error) {
                console.log("Status: " + status);
                console.log("Error: " + error);
                console.log("xhr: " + xhr.readyState);
            },
            statusCode: {
                404: function() {
                    console.log('page not found');
                }
            }
        });
    }
</script>

基本上我想单击另一个视图中的按钮来调用此服务。Community.csthml 中的按钮是:

<button id="btnSpeak" onclick="ttsFunction();">Speak</button>

当我在浏览器中输入 svc 路径时,在 IISEXPRESS 下的 Tracelog 文件夹中发现异常。

例外是:

[HttpException]: The controller for path &amp;#39;/wscccService.svc/&amp;#39; was not found or  does not implement IController.
  at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
  at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
  at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController&amp; controller, IControllerFactory&amp; factory)
  at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
  at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
  at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
 --&gt;</Data>

服务本身很简单:

namespace service
{
   [ServiceContract]
   public interface Iwservice
   {
      [OperationContract]
      [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "RunTts")]
    string RunTts(string text);
   }
 }

为了节省空间,除非您需要,否则我不会发布 web.config。感谢帮助。

4

1 回答 1

1

您的/wscccService.svc文件似乎驻留在 ASP.NET MVC 应用程序目录中。MVC 的路由会拦截请求并开始寻找一个名为 的控制器wscccService.svc,但它找不到。

只需将忽略路由添加到您的路由配置中。

于 2013-07-16T14:14:28.537 回答