gpt4 book ai didi

c# - 使用 JavaScript 连接到 .NET 服务器

转载 作者:太空狗 更新时间:2023-10-30 01:07:10 25 4
gpt4 key购买 nike

我应该从我们的服务器使用 JS 访问资源。我已经设置了下面的代码。

var targetUrl = "http://somePlace.com/actualResourceName";
var xdr = new XDomainRequest();
xdr.onload = function () { alert(xdr.responseText); }
xdr.open("GET", targetUrl);
xdr.send();

但是,我不清楚另一端的方法需要如何创建。我提出了以下建议,完全意识到它不起作用。例如,我确定我缺少正确的属性。我什至不确定在哪里设置该方法应该对 actualResourceName...

使用react
[???]
public String ActualResourceName()
{
return "Bye, bye, cruel word!";
}

我用谷歌搜索过,但没有找到任何解决方案。不过,我可能无意中发现了它,但没有意识到它很有用。

C#中的方法应该怎么写?

最佳答案

好的,到此为止。我将描述两种完成它的方法,最简单的方法:

1. ASP.NET WebApi

您必须创建一个新的 ASP.NET MVC4 项目(无论是发布还是 RC),选择“WebApi”作为选项: Project startup

您将准备好模板。现在你右击“Controllers”文件夹,然后添加 -> Controller : enter image description here

并填写它,例如像那样:

public class ActualResourceController : ApiController
{
public string Get()
{
return "Hey there! Getting the resource...";
}
}

默认路由在 Global.asax 中,当您转到 WebApiConfig.Register(...) 方法的定义时,您会看到默认路由是 host/api/ Controller 。让我们尝试一下,当您启动项目并进入(在我的例子中,端口由开发服务器自动选择)http://localhost:23030/api/ActualResource你会得到:

<string>Hey there! Getting the resource...</string>

WebApi 根据 Accept header 返回 JSON 或 XML,如果您希望 JSON 是唯一的/默认的,请使用 a look at this link.

您当然可以创建一个类并返回它,它将以类似于您将在下面使用 ServiceStack 看到的方式序列化为 XML/JSON。


2. ServiceStack

现在 ServiceStack 是功能强大的开源 REST 网络服务框架。它的工作方式与 WebApi 略有不同,这里有一个快速介绍(尽管文档很好):

创建常规的 ASP.NET MVC 项目(在我的例子中是 MVC4)——您将拥有一个空模板:

service stack startup project

然后启动包管理器控制台并键入(如文档建议的那样)Install-Package ServiceStack.Host.Mvc,这将为您提供一个带有教程应用程序的 ServiceStack 项目模板,您可以使用它如果您愿意,可以稍后删除。

但首先,ServiceStack 在 DTO(请求-响应对象)上工作。因此,让我们创建它们,ActualResource 类将用作请求,ActualResourceResponse 类将用作响应。由于您在请求中没有参数,因此第一个参数很简单:

public class ActualResource
{
}

任何参数都是自动属性。现在响应:

public class ActualResourceResponse
{
public string ResourceName { get; set; }
}

以及服务类本身:

public class ActualResourceService : Service
{
public object Get(ActualResource request)
{
return new ActualResourceResponse {
ResourceName = "Hi! It's the resource name." };
}
}

你当然可以为你当前的目的返回裸string,它会工作一样。

现在在ServiceStack创建的模板中,一切都发生在AppHost.cs文件中,让我们看一下并稍微修改一下:

Routes
.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name*}")
.Add<Todo>("/todos")
.Add<Todo>("/todos/{Id}") //everything up to here are a template/tutorial routes, you can safely remove them
.Add<ActualResource>("/actualResource"); //and here you add a route to your own service

要使其正常工作,您必须转到 Global.asax 并注释掉整个 WebApiConfig.Register(GlobalConfiguration.Configuration) 行,然后进入 RouteConfig.RegisterRoutes 方法并添加:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("api/{*pathInfo}"); // <<<---- this line
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

需要多一点管道,但还不错。

现在当你启动服务时,进入localhost:whateverport/api/actualResource,你会得到熟悉的字符串,下面是截图: ServiceStack reply page

ServiceStack 可以序列化为各种格式,因此如果您访问 http://localhost:yourPort/api/actualResource?format=json,您将获得:

{"resourceName":"Hi! It's the resource name."}

如果 ?format=xml,则:

<ActualResourceResponse>
<ResourceName>Hi! It's the resource name.</ResourceName>
</ActualResourceResponse>

等等……

现在 ServiceStack 的设置有点复杂,但它支持例如开箱即用的 Memcache,您可以在 Redis 中使用,您可以使用各种身份验证提供程序,所有这些在某些情况下可能非常有用。但是,正如本叔叔曾经说过的那样,“能力越大,责任越大”,并且设置阶段更难......


现在您可以随意选择,恕我直言,这两个是目前最简单的选项。当然,这只是一个简单的入门教程,您将有机会在开始项目时深入探索这个主题。

关于c# - 使用 JavaScript 连接到 .NET 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13308583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com