揭露从Intranet内现有的asp.net MVC的网站给其他REST客户打了几个电话? [英] Exposing a few calls from an existing asp.net-mvc site to other REST clients within an intranet?

查看:122
本文介绍了揭露从Intranet内现有的asp.net MVC的网站给其他REST客户打了几个电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的asp.net MVC的网站,现在我需要公开我的几个电话来,仅用于我的网站内现在外部应用程序。这是一个Intranet内的所有发生的事情我公司之内。

I have an existing asp.net-mvc web site and now I need to expose of a few of my calls to external applications that are only used within my site right now. This is all happening within an intranet within my company.

我已阅读此页面这也解释了网页API与控制器动作如这似乎也有类似的问题,这个问题SOF 但答案似乎有点过时。所以我试图确定给出的最新提供的功能是什么,以满足我的要求最简单的解决方案。

I have read this page which explains Web API versus controller actions as well as this SOF question which seems to have a similar issue but the answers seem a bit outdated. So I am trying to determine given the latest available functionality what is the simplest solution to meet my requirement.

在我的情况,因为我已经有我目前的网站中使用相同的控制器操作,然后WEB API并没有真正意义,但如果我谷歌围绕asp.net-MVC认证或安全任何东西,我只看到周围的Web API文章。

In my case, since I already have the same controller actions used within my current website then WEB API doesn't really make sense but if I google anything around asp.net-mvc authentication or security I only see articles around web API.

鉴于此,我试图找出最佳做法暴露我的控制器动作到另一个应用程序。

Given that, I am trying to figure out best practice for exposing my controller action to another application.

推荐答案

在一个理想的世界,你会转换应用的Web API控制器,​​别人建议,但要更加务实,你可以实现,你只公开一个临时解决方案通过扩展需要调用 ApiController

In an ideal world you would convert the app to web api controllers as someone else suggested but to be more pragmatic you can implement an interim solution where you expose only the required calls via extending ApiController

您没有提及当前的应用程序正在使用的MVC的版本也没有你提到的电流控制器如何将数据返回到Web应用程序。
因此,我会假设你通过一个视图模型和剃须刀视图返回的数据。例如:

You did not mention which version of MVC your current app is using nor did you mention how your current Controllers return data to the web app. I will therefore assume you return data via a view model and razor views. eg:

public class ProductsController : Controller
{
      public void Index()
      { 
            var view = new ProductsListView();
            view.Products = _repository.GetProducts();
            return View(view);
      }
}

假设现在你想通过一个REST的API一样,露出的产品列表?
首先检查你的web API安装(通过的NuGet)

Suppose now you want to expose the products list via a REST-like api? First check you have web api installed (via nuget)

安装封装Microsoft.AspNet.WebApi

Install-Package Microsoft.AspNet.WebApi

(再次声明,我不知道版本asp.net的你是什么样的等等这一过程可能版本之间的不同)

(again i'm not sure what ver of asp.net you are on so this process may differ between versions)

现在在你的公共无效的Application_Start()

GlobalConfiguration.Configure(WebApiConfig.Register);//add this before! line below
RouteConfig.RegisterRoutes(RouteTable.Routes);//this line shld already exist

WebApiConfig

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

我想创建一个专用的文件夹的所谓ApiControllers并使用相同的名称添加了控制器;这样你可以有相同名称的控制器,因为它们是在不同的命名空间:

I like to create a dedicated folder called ApiControllers and add controllers with the same name; this way you can have controllers with the same names as they are in different namespaces:

namespace YourApp.Web.ApiControllers
{
    [AllowAnonymous]
    public class ProductsController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage Products()
        {
              var result = new ProductResult();//you could also use the view class ProductsListView
              result.Products = _repository.GetProducts();
              return Request.CreateResponse(httpStatusCode, result);
        }
    }
}

您可以然后通过 yourapp.com/api/products

NB,尽量减少code控制器内的重复 - 这可以通过提取通用零部件到服务类中acheived。

nb, try to reduce duplication of code inside controllers - this can be acheived by extracting common parts into service classes.

这篇关于揭露从Intranet内现有的asp.net MVC的网站给其他REST客户打了几个电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆