[英]Ajax enabled WCF in MVC returns 404
我試圖將WCF服務添加到我的MVC項目中,以便放置我的ajax方法,但是當我嘗試僅通過瀏覽url(例如../Service.svc/HelloWorld)來調用其方法時,或者用jquery ajax調用它,我得到一個404頁面。
如果我在webforms項目中做同樣的事情,它可以正常工作。 我在這里做錯了什么? 這不是保留Ajax方法的最佳實踐地方嗎? 就像我說的那樣:我想在一個通用的地方保留可能在多個視圖中重用的ajax方法,因此,如果可能的話,我不想將其放在視圖的控制器中
我的代碼:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1:IService1
{
public string HelloWorld()
{
return "HelloWorld";
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet]
string HelloWorld();
}
我的配置:
<system.serviceModel>
<services>
<service name="WebApplication1.Service1">
<endpoint address=""
behaviorConfiguration="AJAXEndpoint"
binding="webHttpBinding"
contract="WebApplication1.IService1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="AJAXEndpoint">
<webHttp />
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="1" />
</system.serviceModel>
您必須在Global.asax中添加ServiceRoute 。 您只有一個404,因為該請求沒有路由。
例如,這可能是
routes.Add(new ServiceRoute("ThePath", new WebServiceHostFactory(), typeof(YourService)));
asp.net Web Api +1:這絕對是在asp.net中構建Web api的推薦方法,尤其是在現有的mvc項目中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.