我在_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>
基本上我想点击另一个View中的按钮来调用此服务。 Community.csthml中的按钮是:
<button id="btnSpeak" onclick="ttsFunction();">Speak</button>
当我在浏览器中输入svc路径时,我在IISEXPRESS下的Tracelog文件夹中发现了一个异常。
例外是:
[HttpException]: The controller for path &#39;/wscccService.svc/&#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& controller, IControllerFactory& 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& completedSynchronously)
--></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,除非你需要它。谢谢你的帮助。
答案 0 :(得分:1)
您的/wscccService.svc
文件似乎位于ASP.NET MVC应用程序目录中。 MVC的路由将拦截请求并开始寻找一个名为wscccService.svc
的控制器,它无法找到。
只需add an ignore route到您的路线配置。