如何在 ASP.NET MVC 中使用通用处理程序 (ASHX)?

发布于 2024-07-15 02:18:47 字数 423 浏览 9 评论 0原文

我需要在 ASP.NET MVC 应用程序中使用图像生成器,但我不知道是否可以在 ASP.NET MVC 中使用 ASHX。

如果有一个类似于 IHttpHandler 的类继承了 IViewDataContainer,那么我可以在我的 ASSX 上使用 ViewData.Model。

我读了一篇关于它的博客文章但从我在 ASP.NET 论坛 中获得的信息来看,它似乎已经过时了。

有什么想法吗?

I need a image generator in my ASP.NET MVC application, but I don't know if it is possible to use ASHX in ASP.NET MVC.

If there is an class similar to IHttpHandler that inherits IViewDataContainer, then I could use ViewData.Model on my ASHX.

I read a blog post about it but it seem outdated from the info I got in the ASP.NET forum

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離人涙 2024-07-22 02:18:47

您还可以使用此 ImageResult 类返回图像。

不需要处理程序,只需创建一个路由,例如:

/ImageGenerator/{action}/{imageid}/

并返回请求的 ImageResult。

 public class ImageResult : ActionResult
    {
        public string SourceFilename { get; set; }
        public MemoryStream SourceStream { get; set; }
        public string ContentType { get; set; }
        public ImageResult(string sourceFilename)
        {
            SourceFilename = sourceFilename;
            ContentType = FileTypeHelper.GetContentType(SourceFilename);
        }
        public ImageResult(MemoryStream sourceStream, string contentType)
        {
            SourceStream = sourceStream;
            ContentType = contentType;
        }
        public override void ExecuteResult(ControllerContext context)
        {   
            var res = context.HttpContext.Response;
            res.Clear();
            res.Cache.SetCacheability(HttpCacheability.NoCache);
            res.ContentType = ContentType;

            if (SourceStream != null)
            {
                SourceStream.WriteTo(res.OutputStream);

            }
            else
            {
               res.TransmitFile(SourceFilename);
            }

        }


    }

要使用此功能,请在控制器中执行以下操作并为其添加路由:

public ImageResult GetImage(int imageId)
{
   // Database fetch of image details
   var imageInfo = repository.Get<ImageInfo>(imageId);
   return new ImageResult(imageInfo.FullFilename);
}

也添加了 FileType 帮助器类:

 public static class FileTypeHelper
{
    public static string GetContentType(string SourceFileName)
    {
        var extension = Path.GetExtension(SourceFileName).ToLower();
         switch (extension)
        {
            case ".ai": return "application/postscript";
            case ".aif": return "audio/x-aiff";
            case ".aifc": return "audio/x-aiff";
            case ".aiff": return "audio/x-aiff";
            case ".asc": return "text/plain";
            case ".au": return "audio/basic";
            case ".avi": return "video/x-msvideo";
            case ".bcpio": return "application/x-bcpio";
            case ".bin": return "application/octet-stream";
            case ".c": return "text/plain";
            case ".cc": return "text/plain";
            case ".ccad": return "application/clariscad";
            case ".cdf": return "application/x-netcdf";
            case ".class": return "application/octet-stream";
            case ".cpio": return "application/x-cpio";
            case ".cpp": return "text/plain";
            case ".cpt": return "application/mac-compactpro";
            case ".cs": return "text/plain";
            case ".csh": return "application/x-csh";
            case ".css": return "text/css";
            case ".dcr": return "application/x-director";
            case ".dir": return "application/x-director";
            case ".dms": return "application/octet-stream";
            case ".doc": return "application/msword";
            case ".drw": return "application/drafting";
            case ".dvi": return "application/x-dvi";
            case ".dwg": return "application/acad";
            case ".dxf": return "application/dxf";
            case ".dxr": return "application/x-director";
            case ".eps": return "application/postscript";
            case ".etx": return "text/x-setext";
            case ".exe": return "application/octet-stream";
            case ".ez": return "application/andrew-inset";
            case ".f": return "text/plain";
            case ".f90": return "text/plain";
            case ".fli": return "video/x-fli";
            case ".flv": return "video/x-flv";
            case ".gif": return "image/gif";
            case ".gtar": return "application/x-gtar";
            case ".gz": return "application/x-gzip";
            case ".h": return "text/plain";
            case ".hdf": return "application/x-hdf";
            case ".hh": return "text/plain";
            case ".hqx": return "application/mac-binhex40";
            case ".htm": return "text/html";
            case ".html": return "text/html";
            case ".ice": return "x-conference/x-cooltalk";
            case ".ief": return "image/ief";
            case ".iges": return "model/iges";
            case ".igs": return "model/iges";
            case ".ips": return "application/x-ipscript";
            case ".ipx": return "application/x-ipix";
            case ".jpe": return "image/jpeg";
            case ".jpeg": return "image/jpeg";
            case ".jpg": return "image/jpeg";
            case ".js": return "application/x-javascript";
            case ".kar": return "audio/midi";
            case ".latex": return "application/x-latex";
            case ".lha": return "application/octet-stream";
            case ".lsp": return "application/x-lisp";
            case ".lzh": return "application/octet-stream";
            case ".m": return "text/plain";
            case ".man": return "application/x-troff-man";
            case ".me": return "application/x-troff-me";
            case ".mesh": return "model/mesh";
            case ".mid": return "audio/midi";
            case ".midi": return "audio/midi";
            case ".mime": return "www/mime";
            case ".mov": return "video/quicktime";
            case ".movie": return "video/x-sgi-movie";
            case ".mp2": return "audio/mpeg";
            case ".mp3": return "audio/mpeg";
            case ".mpe": return "video/mpeg";
            case ".mpeg": return "video/mpeg";
            case ".mpg": return "video/mpeg";
            case ".mpga": return "audio/mpeg";
            case ".ms": return "application/x-troff-ms";
            case ".msh": return "model/mesh";
            case ".nc": return "application/x-netcdf";
            case ".oda": return "application/oda";
            case ".pbm": return "image/x-portable-bitmap";
            case ".pdb": return "chemical/x-pdb";
            case ".pdf": return "application/pdf";
            case ".pgm": return "image/x-portable-graymap";
            case ".pgn": return "application/x-chess-pgn";
            case ".png": return "image/png";
            case ".pnm": return "image/x-portable-anymap";
            case ".pot": return "application/mspowerpoint";
            case ".ppm": return "image/x-portable-pixmap";
            case ".pps": return "application/mspowerpoint";
            case ".ppt": return "application/mspowerpoint";
            case ".ppz": return "application/mspowerpoint";
            case ".pre": return "application/x-freelance";
            case ".prt": return "application/pro_eng";
            case ".ps": return "application/postscript";
            case ".qt": return "video/quicktime";
            case ".ra": return "audio/x-realaudio";
            case ".ram": return "audio/x-pn-realaudio";
            case ".ras": return "image/cmu-raster";
            case ".rgb": return "image/x-rgb";
            case ".rm": return "audio/x-pn-realaudio";
            case ".roff": return "application/x-troff";
            case ".rpm": return "audio/x-pn-realaudio-plugin";
            case ".rtf": return "text/rtf";
            case ".rtx": return "text/richtext";
            case ".scm": return "application/x-lotusscreencam";
            case ".set": return "application/set";
            case ".sgm": return "text/sgml";
            case ".sgml": return "text/sgml";
            case ".sh": return "application/x-sh";
            case ".shar": return "application/x-shar";
            case ".silo": return "model/mesh";
            case ".sit": return "application/x-stuffit";
            case ".skd": return "application/x-koan";
            case ".skm": return "application/x-koan";
            case ".skp": return "application/x-koan";
            case ".skt": return "application/x-koan";
            case ".smi": return "application/smil";
            case ".smil": return "application/smil";
            case ".snd": return "audio/basic";
            case ".sol": return "application/solids";
            case ".spl": return "application/x-futuresplash";
            case ".src": return "application/x-wais-source";
            case ".step": return "application/STEP";
            case ".stl": return "application/SLA";
            case ".stp": return "application/STEP";
            case ".sv4cpio": return "application/x-sv4cpio";
            case ".sv4crc": return "application/x-sv4crc";
            case ".swf": return "application/x-shockwave-flash";
            case ".t": return "application/x-troff";
            case ".tar": return "application/x-tar";
            case ".tcl": return "application/x-tcl";
            case ".tex": return "application/x-tex";
            case ".tif": return "image/tiff";
            case ".tiff": return "image/tiff";
            case ".tr": return "application/x-troff";
            case ".tsi": return "audio/TSP-audio";
            case ".tsp": return "application/dsptype";
            case ".tsv": return "text/tab-separated-values";
            case ".txt": return "text/plain";
            case ".unv": return "application/i-deas";
            case ".ustar": return "application/x-ustar";
            case ".vcd": return "application/x-cdlink";
            case ".vda": return "application/vda";
            case ".vrml": return "model/vrml";
            case ".wav": return "audio/x-wav";
            case ".wrl": return "model/vrml";
            case ".xbm": return "image/x-xbitmap";
            case ".xlc": return "application/vnd.ms-excel";
            case ".xll": return "application/vnd.ms-excel";
            case ".xlm": return "application/vnd.ms-excel";
            case ".xls": return "application/vnd.ms-excel";
            case ".xlw": return "application/vnd.ms-excel";
            case ".xml": return "text/xml";
            case ".xpm": return "image/x-xpixmap";
            case ".xwd": return "image/x-xwindowdump";
            case ".xyz": return "chemical/x-pdb";
            case ".zip": return "application/zip";
            default: return string.Format("application/{0}", extension);
        }
    }
}

You can also use this ImageResult class to return an image.

No need for a handler just create a route such as:

/ImageGenerator/{action}/{imageid}/

and return an ImageResult for the request.

 public class ImageResult : ActionResult
    {
        public string SourceFilename { get; set; }
        public MemoryStream SourceStream { get; set; }
        public string ContentType { get; set; }
        public ImageResult(string sourceFilename)
        {
            SourceFilename = sourceFilename;
            ContentType = FileTypeHelper.GetContentType(SourceFilename);
        }
        public ImageResult(MemoryStream sourceStream, string contentType)
        {
            SourceStream = sourceStream;
            ContentType = contentType;
        }
        public override void ExecuteResult(ControllerContext context)
        {   
            var res = context.HttpContext.Response;
            res.Clear();
            res.Cache.SetCacheability(HttpCacheability.NoCache);
            res.ContentType = ContentType;

            if (SourceStream != null)
            {
                SourceStream.WriteTo(res.OutputStream);

            }
            else
            {
               res.TransmitFile(SourceFilename);
            }

        }


    }

To use this do the following in a controller and add a route for it:

public ImageResult GetImage(int imageId)
{
   // Database fetch of image details
   var imageInfo = repository.Get<ImageInfo>(imageId);
   return new ImageResult(imageInfo.FullFilename);
}

Have added the FileType helper class too:

 public static class FileTypeHelper
{
    public static string GetContentType(string SourceFileName)
    {
        var extension = Path.GetExtension(SourceFileName).ToLower();
         switch (extension)
        {
            case ".ai": return "application/postscript";
            case ".aif": return "audio/x-aiff";
            case ".aifc": return "audio/x-aiff";
            case ".aiff": return "audio/x-aiff";
            case ".asc": return "text/plain";
            case ".au": return "audio/basic";
            case ".avi": return "video/x-msvideo";
            case ".bcpio": return "application/x-bcpio";
            case ".bin": return "application/octet-stream";
            case ".c": return "text/plain";
            case ".cc": return "text/plain";
            case ".ccad": return "application/clariscad";
            case ".cdf": return "application/x-netcdf";
            case ".class": return "application/octet-stream";
            case ".cpio": return "application/x-cpio";
            case ".cpp": return "text/plain";
            case ".cpt": return "application/mac-compactpro";
            case ".cs": return "text/plain";
            case ".csh": return "application/x-csh";
            case ".css": return "text/css";
            case ".dcr": return "application/x-director";
            case ".dir": return "application/x-director";
            case ".dms": return "application/octet-stream";
            case ".doc": return "application/msword";
            case ".drw": return "application/drafting";
            case ".dvi": return "application/x-dvi";
            case ".dwg": return "application/acad";
            case ".dxf": return "application/dxf";
            case ".dxr": return "application/x-director";
            case ".eps": return "application/postscript";
            case ".etx": return "text/x-setext";
            case ".exe": return "application/octet-stream";
            case ".ez": return "application/andrew-inset";
            case ".f": return "text/plain";
            case ".f90": return "text/plain";
            case ".fli": return "video/x-fli";
            case ".flv": return "video/x-flv";
            case ".gif": return "image/gif";
            case ".gtar": return "application/x-gtar";
            case ".gz": return "application/x-gzip";
            case ".h": return "text/plain";
            case ".hdf": return "application/x-hdf";
            case ".hh": return "text/plain";
            case ".hqx": return "application/mac-binhex40";
            case ".htm": return "text/html";
            case ".html": return "text/html";
            case ".ice": return "x-conference/x-cooltalk";
            case ".ief": return "image/ief";
            case ".iges": return "model/iges";
            case ".igs": return "model/iges";
            case ".ips": return "application/x-ipscript";
            case ".ipx": return "application/x-ipix";
            case ".jpe": return "image/jpeg";
            case ".jpeg": return "image/jpeg";
            case ".jpg": return "image/jpeg";
            case ".js": return "application/x-javascript";
            case ".kar": return "audio/midi";
            case ".latex": return "application/x-latex";
            case ".lha": return "application/octet-stream";
            case ".lsp": return "application/x-lisp";
            case ".lzh": return "application/octet-stream";
            case ".m": return "text/plain";
            case ".man": return "application/x-troff-man";
            case ".me": return "application/x-troff-me";
            case ".mesh": return "model/mesh";
            case ".mid": return "audio/midi";
            case ".midi": return "audio/midi";
            case ".mime": return "www/mime";
            case ".mov": return "video/quicktime";
            case ".movie": return "video/x-sgi-movie";
            case ".mp2": return "audio/mpeg";
            case ".mp3": return "audio/mpeg";
            case ".mpe": return "video/mpeg";
            case ".mpeg": return "video/mpeg";
            case ".mpg": return "video/mpeg";
            case ".mpga": return "audio/mpeg";
            case ".ms": return "application/x-troff-ms";
            case ".msh": return "model/mesh";
            case ".nc": return "application/x-netcdf";
            case ".oda": return "application/oda";
            case ".pbm": return "image/x-portable-bitmap";
            case ".pdb": return "chemical/x-pdb";
            case ".pdf": return "application/pdf";
            case ".pgm": return "image/x-portable-graymap";
            case ".pgn": return "application/x-chess-pgn";
            case ".png": return "image/png";
            case ".pnm": return "image/x-portable-anymap";
            case ".pot": return "application/mspowerpoint";
            case ".ppm": return "image/x-portable-pixmap";
            case ".pps": return "application/mspowerpoint";
            case ".ppt": return "application/mspowerpoint";
            case ".ppz": return "application/mspowerpoint";
            case ".pre": return "application/x-freelance";
            case ".prt": return "application/pro_eng";
            case ".ps": return "application/postscript";
            case ".qt": return "video/quicktime";
            case ".ra": return "audio/x-realaudio";
            case ".ram": return "audio/x-pn-realaudio";
            case ".ras": return "image/cmu-raster";
            case ".rgb": return "image/x-rgb";
            case ".rm": return "audio/x-pn-realaudio";
            case ".roff": return "application/x-troff";
            case ".rpm": return "audio/x-pn-realaudio-plugin";
            case ".rtf": return "text/rtf";
            case ".rtx": return "text/richtext";
            case ".scm": return "application/x-lotusscreencam";
            case ".set": return "application/set";
            case ".sgm": return "text/sgml";
            case ".sgml": return "text/sgml";
            case ".sh": return "application/x-sh";
            case ".shar": return "application/x-shar";
            case ".silo": return "model/mesh";
            case ".sit": return "application/x-stuffit";
            case ".skd": return "application/x-koan";
            case ".skm": return "application/x-koan";
            case ".skp": return "application/x-koan";
            case ".skt": return "application/x-koan";
            case ".smi": return "application/smil";
            case ".smil": return "application/smil";
            case ".snd": return "audio/basic";
            case ".sol": return "application/solids";
            case ".spl": return "application/x-futuresplash";
            case ".src": return "application/x-wais-source";
            case ".step": return "application/STEP";
            case ".stl": return "application/SLA";
            case ".stp": return "application/STEP";
            case ".sv4cpio": return "application/x-sv4cpio";
            case ".sv4crc": return "application/x-sv4crc";
            case ".swf": return "application/x-shockwave-flash";
            case ".t": return "application/x-troff";
            case ".tar": return "application/x-tar";
            case ".tcl": return "application/x-tcl";
            case ".tex": return "application/x-tex";
            case ".tif": return "image/tiff";
            case ".tiff": return "image/tiff";
            case ".tr": return "application/x-troff";
            case ".tsi": return "audio/TSP-audio";
            case ".tsp": return "application/dsptype";
            case ".tsv": return "text/tab-separated-values";
            case ".txt": return "text/plain";
            case ".unv": return "application/i-deas";
            case ".ustar": return "application/x-ustar";
            case ".vcd": return "application/x-cdlink";
            case ".vda": return "application/vda";
            case ".vrml": return "model/vrml";
            case ".wav": return "audio/x-wav";
            case ".wrl": return "model/vrml";
            case ".xbm": return "image/x-xbitmap";
            case ".xlc": return "application/vnd.ms-excel";
            case ".xll": return "application/vnd.ms-excel";
            case ".xlm": return "application/vnd.ms-excel";
            case ".xls": return "application/vnd.ms-excel";
            case ".xlw": return "application/vnd.ms-excel";
            case ".xml": return "text/xml";
            case ".xpm": return "image/x-xpixmap";
            case ".xwd": return "image/x-xwindowdump";
            case ".xyz": return "chemical/x-pdb";
            case ".zip": return "application/zip";
            default: return string.Format("application/{0}", extension);
        }
    }
}
山人契 2024-07-22 02:18:47

您应该能够从操作中返回图像? 请参阅此处了解更多信息。

是的,您可以将 ashx 与 MVC 一起使用(通过告诉它忽略路由) - 但我不确定这是最好的方法,除非它是重复使用现有代码。


抱歉各位,链接已被删除。 它发生了。

You should just be able to return the image from the action? See here for more.

And yes, you can use ashx alongside MVC (by telling it to ignore the route) - but I'm not sure that is the best approach unless it is to re-use existing code.


Sorry guys, the link has been removed. It happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文