简体   繁体   English

Umbraco 从 url 获取媒体

[英]Umbraco get media from url

I want to protect my media by a genericHandler.ashx.我想通过 genericHandler.ashx 保护我的媒体。 I have a rewrite rule /media to /handlers/my-handler.ashx?media-url=xxx.jpg But I cannot find a function in umbraco api to get nodeId from url.我有一个重写规则 /media 到 /handlers/my-handler.ashx?media-url=xxx.jpg 但我在 umbraco api 中找不到 function 以从 Z572D4E4121E5E6B711112A 获取 nodeId。

At the moment I use this sql query:目前我使用这个 sql 查询:

public static Int32 GetNodeIdFromUrl(String url)
{
        SqlParameter[] sqParams = { new SqlParameter("@url", url) };
        string sql = "select contentNodeId from cmsPropertyData where dataNvarchar =     @url";
        int id = -1;
        try
        {

           id = (int)SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,sql,sqParams);
        }
        catch (Exception ex)
        {
            umbraco.BusinessLogic.Log.Add(
                umbraco.BusinessLogic.LogTypes.Error, 
                new umbraco.BusinessLogic.User(0), 0,
                "Error from download security handler ->" + ex.Message.ToString());
        }

        return id;
    }

inspired by http://our.umbraco.org/forum/developers/api-questions/4284-getting-media-Id-from-path灵感来自http://our.umbraco.org/forum/developers/api-questions/4284-getting-media-Id-from-path

do you know any umbraco api function to do this before?你知道任何 umbraco api function 之前这样做吗?

i think if you know the correct format of the url, you can take the media id by using some regular expression or by splitting the url.我想如果你知道 url 的正确格式,你可以通过使用一些正则表达式或拆分 url 来获取媒体 ID。

If you have a redirect set up to map anything from /media to /handlers/my-handler.ashx, then the original URL request will be in the format /media/{mediaID}/{fileName}.{extension}.如果您将重定向设置为 map,从 /media 到 /handlers/my-handler.ashx,则原始 URL 请求将采用 /media/{mediaID}/{fileName}.{extension} 格式。 The media ID is the name of the folder inside the /media directory on disk.媒体 ID 是磁盘上 /media 目录中文件夹的名称。 When you get an incoming request, simply SubString everything after "/media/" and before the next "/".当您收到传入请求时,只需 SubString 在“/media/”之后和下一个“/”之前的所有内容。

HTH,高温下,

Benjamin本杰明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM