开发者

MVC3 routing always return file no matter what directory it's requested from

开发者 https://www.devze.com 2023-04-11 19:16 出处:网络
So I found this image upload tool on the web.It\'s pretty slick, is built in flash, will resize imag开发者_如何学Goes before uploading etc.Basically it\'ll save me a lot of time if I don\'t have to im

So I found this image upload tool on the web. It's pretty slick, is built in flash, will resize imag开发者_如何学Goes before uploading etc. Basically it'll save me a lot of time if I don't have to implement the functionality it provides myself.

It all looks pretty good except that I want to be able to include the swf file on several of my pages with different URL's. Problem is the flash object given to me tries to load an xml file that it expects to be in the same directory as your web page and there's no way for me to change the directory it expects the file in. Thus I was wondering if there's a way to do the following.

I'm running MVC3 and I want to return an xml file no matter where it's requested from as if you'd requested it directly. so mysite.com/test.xml, mysite.com/someurl/test.xml, mysite.com/some/long/nested/url/test.xml will all return test.xml that doesn't actually reside in any of those places but in a public content folder with all my other javascripts and css files etc...


I haven't tested this but try something like

routes.MapPageRoute("xmlroute", "{anything}.xml", "~/yourfile.xml");

or

routes.MapPageRoute("xmlroute", "{*}.xml", "~/yourfile.xml");

or you can try


routes.MapRoute(
    "xmlroute",
    "{whatever}.xml",
    new { controller = "FileHandler", action = "XmlFile"}
); 

Your controller action can then just return the File(yourXmlFile) result.


First, define the following Route before the Default route:

routes.MapRoute(
    "XML",
    "{*pathAndFile}",
    new { controller = "Content", action = "Index" },
    new { pathAndFile = @"^.+\.xml$" }
);

Next, create ContentController, with the following Index action:

public ActionResult Index(string pathAndFile)
{
    var filePath = Request.MapPath("~/Content/" + pathAndFile.Split(new string[] { @"/" }, StringSplitOptions.None).Last());
    if (System.IO.File.Exists(filePath))
        return File(filePath, "text/xml");

    return null;
}

This will route any request ending in ".xml" to the ContentController, which will check to see if the xml file exists, and will serve the requested file if it exists.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号