如何在javascript中访问控制器变量

时间:2016-06-14 09:31:21

标签: javascript c# model-view-controller reactjs jsx

嘿伙计我想把我在控制器中创建的一些变量(LinkImg,Titulo,descricao,LinkIcon)称为View,但我不知道该怎么做。你能帮助我吗?感谢。

这是我的控制器代码:

using HtmlAgilityPack;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using SoftInsa.Link.Web.Api.Models;
using SoftInsa.Link.Web.Api.Searches;
using SoftInsa.Link.Web.App_Start;
using SoftInsa.Link.Web.Authentication.Models;
using SoftInsa.Link.Web.Models.Timeline;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace SoftInsa.Link.Web.Api
{
    [Authorize, RoutePrefix("api/timeline")]
    public class TimelineController : ApiController
    {
        [HttpGet, Route("{id:objectid}")]
        public IHttpActionResult GetOne(string id)
        {
            var db = DbConfig.GetByName(DbNames.SoftInsaLinkDb);

            var post = db.GetCollection<Post>()
                .FindOneById(ObjectId.Parse(id));

            if (post == null) { return NotFound(); }

            var channel = db.GetCollection<Channel>()
                .FindOneById(post.Channel.Id);

            var subscriptions = db.GetCollection<Person>()
                .FindOne(Query<Person>.EQ(p => p.UserName, User.Identity.Name))
                .Subscriptions;

            if (channel.Flags.Contains(ChannelFlag.Restricted) &&
                !subscriptions.Any(s => s.Channel.Id == channel.Id))
            {
                return Content(HttpStatusCode.Forbidden,
                    new ErrorMessage("You cannot access this post because this channel is restricted and you are not subscribed to it."));
            }

            return Ok(post);
        }

        [HttpDelete, Route("{id:objectid}/notification")]
        public IHttpActionResult DeleteNotification(string id)
        {
            var result = DbConfig.GetByName(DbNames.SoftInsaLinkDb)
                .GetCollection<PushNotification>()
                .Remove(Query.And(
                        Query<PushNotification>.EQ(n => n.PostId, ObjectId.Parse(id)),
                        Query<PushNotification>.EQ(n => n.UserName, User.Identity.Name)),
                    RemoveFlags.Single
                );

            if (result.DocumentsAffected == 1)
            {
                return Ok();
            }

            return NotFound();
        }

        [HttpGet, Route("notifications")]
        public IHttpActionResult GetNotifications([FromUri]TimelineSearch search)
        {
            search = search ?? new TimelineSearch();

            var db = DbConfig.GetByName(DbNames.SoftInsaLinkDb);

            var pushNotificationQuery = Query<PushNotification>
                .EQ(n => n.UserName, User.Identity.Name);

            if (search.ChannelId != null)
            {
                var channelFilter = Query<Post>.EQ(p => p.Channel.Id, ObjectId.Parse(search.ChannelId));
                pushNotificationQuery = Query.And(pushNotificationQuery, channelFilter);
            }

            var pushNotifications = db.GetCollection<PushNotification>()
                .Find(pushNotificationQuery);

            var postQuery = Query<Post>.In(p => p.Id, pushNotifications.Select(n => n.PostId));

            if (search.Timestamp != null)
            {
                postQuery = Query.And(
                    postQuery,
                    Query<Post>.GTE(p => p.Date, search.Timestamp.Value));
            }

            var importantPosts = db.GetCollection<Post>()
                .Find(postQuery)
                .SetSortOrder(SortBy<Post>.Descending(p => p.Date))
                .SetLimit(search.Take ?? 50)
                .SetSkip(search.Skip ?? 0);

            return Ok(importantPosts);
        }

        /// <summary>
        /// Returns all the posts in the user's subscriptions,
        /// ordered chronologically.
        /// - 200 is returned with the posts if everything went well.
        /// - 304 is returned with nothing if Timestamp is specified
        ///   and no new posts were created since that date.
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet, Route("")]
        public IHttpActionResult GetAll([FromUri]TimelineSearch search)
        {
            search = search ?? new TimelineSearch();

            var db = DbConfig.GetByName(DbNames.SoftInsaLinkDb);

            // Get the current user's subscriptions.
            var subscriptions = db.GetCollection<Person>()
                .FindOne(Query<Person>.EQ(p => p.UserName, User.Identity.Name))
                .Subscriptions;

            // Posts with push notifications are not treated here,
            // so, we remove them, by finding their ids in the push
            // notification collection and using them in a NotIn clause.
            var postIds = db.GetCollection<PushNotification>()
                .Find(Query<PushNotification>.EQ(n => n.UserName, User.Identity.Name))
                .SetFields(Fields<PushNotification>.Include(n => n.PostId))
                .Select(n => n.PostId);

            var queryArgs = new List<IMongoQuery>
            {
                Query<Post>.In(
                    p => p.Channel.Id,
                    subscriptions.Select(s => s.Channel.Id)),
                Query<Post>.NotIn(p => p.Id, postIds)
            };

            if (search.ChannelId != null)
            {
                queryArgs.Add(Query<Post>.EQ(p => p.Channel.Id, ObjectId.Parse(search.ChannelId)));
            }

            if (search.Timestamp != null)
            {
                queryArgs.Add(Query<Post>.GTE(p => p.Date, search.Timestamp.Value));
            }

            var posts = db.GetCollection<Post>()
                .Find(Query.And(queryArgs))
                .SetSortOrder(SortBy<Post>.Descending(p => p.Date))
                .SetLimit(search.Take ?? 50)
                .SetSkip(search.Skip ?? 0);

            return Ok(posts);
        }

        [HttpGet, Route("sitedata")]
        public async Task<IHttpActionResult> GetDadosSite([FromBody] string link)
        {
            using (var client = new HttpClient())
            {

                List<string> source = new List<string>();

                HtmlWeb web = new HtmlWeb();
                string url = "http://linsa.softinsa.com/Account/Login?ReturnUrl=%2F";
                HtmlDocument document = web.Load(url);
                Uri myUri = new Uri(url);
                string host = myUri.Host;

                var head = document.DocumentNode.SelectSingleNode("//head");
                var meta = head.SelectNodes("//meta").AsEnumerable();
                var urls = document.DocumentNode.SelectNodes("//img")
                        .Select(e => e.GetAttributeValue("src", null))
                        .Where(s => !string.IsNullOrEmpty(s))
                        .Where(s => !s.StartsWith("//"))
                        .Select(s => s.StartsWith("http") ? s : myUri.Scheme + "://" + host + s);

                var titulo = "";
                var descricao = "";
                var linkImg = "";
                var linkIcon = "";
                var linkImgAlt = "";
                var length = 0L;



                var fbProperties = (head.SelectNodes("//meta[contains(@property, 'og:')]") ?? Enumerable.Empty<HtmlNode>())
                    .ToDictionary(n => n.Attributes["property"].Value, n => n.Attributes["content"].Value);


                linkIcon = (head.SelectSingleNode("//link[contains(@rel, 'apple-touch-icon')]")?.Attributes["href"]?.Value) ??
                    (head.SelectSingleNode("//link[contains(@rel, 'icon')]")?.Attributes["href"]?.Value) ??
                    host + "/favicon.ico";


                var title = head.SelectSingleNode("//title")?.InnerText;





                if (fbProperties.TryGetValue("og:title", out titulo) == false || titulo == null)
                {
                    titulo = (title ?? host);
                }

                if (fbProperties.TryGetValue("og:description", out descricao) == false || descricao == null)
                {
                    descricao = ("none");
                }

                if (fbProperties.TryGetValue("og:image", out linkImg) == false || linkImg == null)
                {
                    linkImg = ("none");
                }



                if (linkImg == "none")
                {

                    foreach (var node in urls)
                    {
                        source.Add(node);
                    }
                    foreach (var links in source)
                    {
                        try
                        {
                            var response = client.SendAsync(new HttpRequestMessage
                            {
                                Method = HttpMethod.Head,
                                RequestUri = new Uri(links)
                            }).Result;

                            var fileLength = response.Content.Headers.ContentLength;

                            Console.WriteLine($"{links}: {fileLength} bytes");

                            if (length < fileLength)
                            {
                                linkImg = links;
                                linkImgAlt = links;
                                length = fileLength ?? 0;
                            }

                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }

                    }
                }
                return (null);
            }
        }
    }
}

这是我的观看代码:

'use strict';

import React from 'react/addons';

import { Carousel, CarouselItem } from 'react-bootstrap';

import { Link } from 'react-router';

import cx from 'classnames';

import { user as userAccount } from '../../scripts/account-details';

function getColorForChannel(name) {

    const hue = Array.from(name)
      .map((ch, i) => ch.charCodeAt(0) * (i + 1))
      .reduce((sum, ch) => sum + ch, 0) % 360;

    return `hsl(${hue}, 75%, 75%)`;
}

function urlify(text){
    var urlRegex = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;

    return urlRegex.test(text);
}

function parseLinha(linha) {
    return linha.split(' ')
        .map((x, i) => urlify(x) ? <a key={i} href={x.trim()} target='_blank' rel='nofollow'>{x} </a> : x + " ");
}

/**
 * Usernames that don't really link to people.
 */
const nonPeopleUserNames = ['linsasupport'];

const TimelineItem = React.createClass({

    getDefaultProps() {
        return {
            important: false
        };
    },
    propTypes: {
        routeDepth: React.PropTypes.number,
        router: React.PropTypes.func,
        item: React.PropTypes.object.isRequired,
        onDelete: React.PropTypes.func,
        onLinkClicked: React.PropTypes.func.isRequired,
        important: React.PropTypes.bool
    },
    getChildContext() {
        return {
            routeDepth: this.props.routeDepth,
            router: this.props.router
        };
    },
    childContextTypes: {
        routeDepth: React.PropTypes.number.isRequired,
        router: React.PropTypes.func.isRequired
    },

    getUrlState() {
           return (               

                        <div class="thumbnail">
                        <img src=LinkImg alt="..."/>
                            <div class="caption">
                            <h1>titulo</h1><h6>URL</h6>   
                            <h4>descricao</h4>                         
                    </div>
                </div>
                );
        },

        renderAttachments(attachments) {
            if (!attachments.length) { return null; }

            if (attachments.every((a) => a.Type.toLowerCase() === 'image')) {
                return (
                  <Carousel>
                        {attachments.map((a) =>
                            <CarouselItem key={a.Id}>
                        <a href={`timeline/${this.props.item.Id}/attachments/${a.Id}`} target='_blank'>
                          <div style={{
            backgroundImage: `url(/timeline/${this.props.item.Id}/attachments/${a.Id})`,
        backgroundSize: 'contain',
        backgroundPosition: 'center center',
        height: 300, width: '100%', backgroundRepeat: 'no-repeat' }} />
              </a>
            </CarouselItem>)}
        </Carousel>
      );
    }

    return (
      <div className='si-timeline-item-attachments'>
        <ul className='fa-ul'>
    {attachments.map((a) =>
            <li key={a.Id}>
              <i className='fa-li fa fa-file' />
              <a href={`/timeline/${this.props.item.Id}/attachments/${a.Id}`}
                target='_blank'>
        {a.Name}
              </a>
            </li>)}
        </ul>
      </div>
    );

              },
  handleDelete() {
    if (typeof (this.props.onDelete) === 'function') {
      this.props.onDelete();
              }
              },
  render() {
    const { item, important } = this.props;

    const itemClasses = cx({
      'si-timeline-item': true,
      'si-urgent': important
              });

    const d = item.Date instanceof Date ? item.Date : new Date(item.Date);

    return (
      <div className={itemClasses}>
        <div className='si-timeline-item-header'>
              {important ?
            <button className='btn btn-default si-delete-btn'
                onClick={this.handleDelete}
                title='Remover notificação'>
              <i className='fa fa-lg fa-remove' />
            </button> : null}
          <div className='si-profile-image-fixed'
            style={{
                backgroundImage: `url(/users/${item.Author.UserName}/profileimage)`
                }} />
                  <div>
                {nonPeopleUserNames.indexOf(item.Author.UserName) === -1 ?
                      <Link className='si-timeline-item-title-fixed'
                        onClick={(e) => this.props.onLinkClicked(e)}
                        to={`/users/${item.Author.UserName}`}>
                    {item.Author.Name} {item.Author.UserName === userAccount.manager ? <small>(Manager)</small> : null}
                      </Link> :
                      <span className='si-timeline-item-title-fixed'>
                    {item.Author.Name} {item.Author.UserName === userAccount.manager ? <small>(Manager)</small> : null}
                      </span>}

                    <date className='si-timeline-item-date-fixed' dateTime={d.toISOString()}>
                    {d.toLocaleString(navigator.language)}
                    </date>
                  </div>

                  <small className='si-timeline-item-channel'
                      style={{borderRight: `7px solid ${getColorForChannel(item.Channel.Name)}`}}>
                {item.Channel.Name}
                  </small>
                </div>
                <div className='si-timeline-item-content'>
                {item.Contents.split('/n')
                        .map((l, i) => {
                        const conteudos = parseLinha(l);

                        console.log(conteudos);
                        return <p key={i}>{conteudos}</p>;

                }
                )}
                {urlify(item.Contents) && this.getUrlState()}
                    </div>
                {this.renderAttachments(item.Attachments)}
              </div>
            );
                }
                });
        export default TimelineItem;

我想调用此函数的变量:

[HttpGet, Route("sitedata")]
        public async Task<IHttpActionResult> GetDadosSite([FromBody] string link)
        {
            using (var client = new HttpClient())
            {

                List<string> source = new List<string>();

                HtmlWeb web = new HtmlWeb();
                string url = "http://linsa.softinsa.com/Account/Login?ReturnUrl=%2F";
                HtmlDocument document = web.Load(url);
                Uri myUri = new Uri(url);
                string host = myUri.Host;

                var head = document.DocumentNode.SelectSingleNode("//head");
                var meta = head.SelectNodes("//meta").AsEnumerable();
                var urls = document.DocumentNode.SelectNodes("//img")
                        .Select(e => e.GetAttributeValue("src", null))
                        .Where(s => !string.IsNullOrEmpty(s))
                        .Where(s => !s.StartsWith("//"))
                        .Select(s => s.StartsWith("http") ? s : myUri.Scheme + "://" + host + s);

                var titulo = "";
                var descricao = "";
                var linkImg = "";
                var linkIcon = "";
                var linkImgAlt = "";
                var length = 0L;



                var fbProperties = (head.SelectNodes("//meta[contains(@property, 'og:')]") ?? Enumerable.Empty<HtmlNode>())
                    .ToDictionary(n => n.Attributes["property"].Value, n => n.Attributes["content"].Value);


                linkIcon = (head.SelectSingleNode("//link[contains(@rel, 'apple-touch-icon')]")?.Attributes["href"]?.Value) ??
                    (head.SelectSingleNode("//link[contains(@rel, 'icon')]")?.Attributes["href"]?.Value) ??
                    host + "/favicon.ico";


                var title = head.SelectSingleNode("//title")?.InnerText;





                if (fbProperties.TryGetValue("og:title", out titulo) == false || titulo == null)
                {
                    titulo = (title ?? host);
                }

                if (fbProperties.TryGetValue("og:description", out descricao) == false || descricao == null)
                {
                    descricao = ("none");
                }

                if (fbProperties.TryGetValue("og:image", out linkImg) == false || linkImg == null)
                {
                    linkImg = ("none");
                }



                if (linkImg == "none")
                {

                    foreach (var node in urls)
                    {
                        source.Add(node);
                    }
                    foreach (var links in source)
                    {
                        try
                        {
                            var response = client.SendAsync(new HttpRequestMessage
                            {
                                Method = HttpMethod.Head,
                                RequestUri = new Uri(links)
                            }).Result;

                            var fileLength = response.Content.Headers.ContentLength;

                            Console.WriteLine($"{links}: {fileLength} bytes");

                            if (length < fileLength)
                            {
                                linkImg = links;
                                linkImgAlt = links;
                                length = fileLength ?? 0;
                            }

                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }

                    }
                }
                return (null);
            }
        }

到这个功能:

getUrlState() {
           return (               

                        <div class="thumbnail">
                        <img src=LinkImg alt="..."/>
                            <div class="caption">
                            <h1>titulo</h1><h6>URL</h6>   
                            <h4>descricao</h4>                         
                    </div>
                </div>
                );
        },

1 个答案:

答案 0 :(得分:1)

更改GetDadosSite操作以返回类似于此

的操作
public async Task<IHttpActionResult> GetDadosSite([FromBody] string link)
{
    //build up your variables 
    //Now place all the variables into a transport object
    VarContainer c = new VarContainer(LinkImg,Titulo,descricao,LinkIcon);
    return Ok(VarContainer);  // Returns an OkNegotiatedContentResult
}

然后在getUrlState中对GetDadosSite进行ajax调用并获取变量