Razor CSS文件位置为Variable

Ero*_*ocM 8 html css iframe razor

我在iframe中包装了一个剃刀视图.剃刀视图是不同域上的Web服务.

这是我在做的事情:

<!DOCTYPE html>
<html>
<body>

<p align="center">
    <img src="http://somewhere.com/images/double2.jpg" />
</p>

<p align="center">
<iframe src="https://secure.somewhereelse.com/MyPortal?CorpID=12334D-4C12-450D-ACB1-7372B9D17C22" width="550" height="600" style="float:middle">
  <p>Your browser does not support iframes.</p>
</iframe>
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是src站点的标题:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/cupertino/jquery-ui-1.8.21.custom.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
</head>
Run Code Online (Sandbox Code Playgroud)

我希望iframe src使用调用站点的CSS.

有没有办法传入CSS URL或让它继承调用站点的CSS?

我甚至认为css文件位置是从原始站点传入的参数.

有人有什么建议吗?

Jam*_*ams 6

您无法使用iframe在您的网站上强制执行您的CSS.css必须包含在iframe中包含的页面源中.它曾经是可能的,但在某些情况下使用javascript,并且页面在同一个域上.

您可以使用自己的CSS的唯一其他方式是,如果Web服务允许您传入CSS的URL.但您必须查阅Web服务的文档才能找到答案.


Mat*_*ott 5

我会将CSS url作为参数传递给iframe's src属性:

<iframe src="http://somedomain.com/?styleUrl=@(ResolveStyleUrl())"></iframe>
Run Code Online (Sandbox Code Playgroud)

ResolveStyleUrl可能被定义为:

@functions {

  public IHtmlString ResolveStyleUrl()
  {
    string url = Url.Content("~/Content/site.css");
    string host = "http" + (Request.IsSecureConnection ? "s" : "") + "//" + Request.Url.Host + url;
    return Raw(url);
  }

}
Run Code Online (Sandbox Code Playgroud)

这当然是假设域将接受样式url查询字符串并<link />在远程页面上呈现相应的?


GLE*_*LES 5

Eroc,很抱歉,您无法使用iframe在其他网站上强制执行您的css,因为大多数浏览器都会给出错误,例如Chrome提供的错误:

不安全的JavaScript尝试使用URL http://terenceford.com/catalog/index.php访问框架?来自URL为http://www.example.com/example.php的框架.域,协议和端口必须匹配.

但这并不意味着您无法从该页面中提取html(可能会根据您的需要进行修改)



http://php.net/manual/en/book.curl.php可用于 http://simplehtmldom.sourceforge.net/的网站报废

首先使用这些功能:

curl_init();
curl_setopt(); 
curl_exec();
curl_close();
Run Code Online (Sandbox Code Playgroud)

然后解析html.

在尝试自己之后,你可以看看我为解析beemp3内容而做的下面这个例子,当我想创建一个用于直接下载歌曲的丰富工具时,遗憾的是我不能因为验证码但它对你有用


目录结构

C:\ WAMP\WWW \尝试

- simple_html_dom.php

- try.php


try.php:

<?php
/*integrate results for dif websites seperately*/
require_once('simple_html_dom.php');
$q='eminem';
$mp3sites=array('http://www.beemp3.com/');
$ch=curl_init("{$mp3sites[0]}index.php?q={$q}&st=all");
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
$result=curl_exec($ch);
curl_close($ch);
$html=str_get_html("{$result}");
$ret = $html->find("a");

echo "<head><style type='text/css'>a:link,a{font-size:16px;font-weight:bold;font-family:helvetica;text-decoration:none;color:#458;}a:hover{color:#67b;text-decoration:underline;}a:visited{color:silver;}</style></head>";
$unik=array(null);
foreach($ret as $link)
{
$find="/(.{1,})(\.php)[?](file=.{1,})&song=(.{1,})/i";
$replace="$4";
if(preg_match("{$find}",$link->href))
    {
    $unik[]=$link->href;
    if(current($unik)===prev($unik)){unset($unik);}
    else{
    echo "<a href='".$mp3sites[0].$link->href."'>".urldecode(preg_replace($find,$replace,$mp3sites[0].$link->href))."</a><br/>";
    }}
}
?>
Run Code Online (Sandbox Code Playgroud)

我知道你不用php编写代码,但我认为你有能力翻译代码.看看这个: php到C#转换器

我花时间在这个问题上,因为只有我能理解提供赏金意味着什么.可能答案似乎无关(因为我没有使用基于javascript或html的解决方案),但由于跨域问题,这对您来说是一个重要的教训.我希望你在c#中找到类似的库.祝你好运