简体   繁体   English

c#Web API在本地可用,但发布后在Azure中不起作用

[英]c# web API locally work, but does not work in Azure after publish

I got below error. 我遇到了错误。 I am trying to implement download WEB API in C# which downloads the blob to file from azure blob storage. 我正在尝试在C#中实现下载WEB API,该API将blob从azure blob存储下载到文件中。

I have tried debugging mode on visual studio but it it doesn't work and return errors when tested locally only get that error on deployment. 我曾在Visual Studio上尝试过调试模式,但它无法正常工作,并且在本地测试时仅在部署时会返回该错误。 i'm guessing it might be the file path but i don't know to be honest. 我猜这可能是文件路径,但我不老实。

Internal Server Error 500. 内部服务器错误500。

[RoutePrefix("api/download")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class DownloadController : ApiController
{
    private ggContext db = new ggContext();
    private const string Container = "ggblobcontainer";
    [HttpGet]
    public HttpResponseMessage GetFile(int audioid)
    {
        //get the object storing the audio 
        Someobject zzz = db.Meetings.Find(audioid);
        //get the filename from the object 
        string fileName = zzz.GetFileName();
        //account information from web.config 
        var accountName = ConfigurationManager.AppSettings["storage:account:name"];
        var accountKey = ConfigurationManager.AppSettings["storage:account:key"];
        var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
        //create blob client from account
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        //get the container with the blobs storing the audio
        CloudBlobContainer audioContainer = blobClient.GetContainerReference(Container);
        //get the specific blob with the filename from object
        CloudBlockBlob blockBlob = audioContainer.GetBlockBlobReference(fileName);
        //if the blob is null error response
        if (blockBlob == null)
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "blob with the file name " + fileName + " does not exist in " + Container);
        }
        try
        {
            //cause audio storage name on azure has "" eg. "sick audio file - why is it wrong [LYRICS].mp3" with quotations
            string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
            Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
            //replace illegal chars with nothing in case replace the . for .mp3 
            string CleanFileName = r.Replace(fileName, "");
            // download to desktop
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //change it to fileName not dragon little bits 
            string gg = Path.Combine(path, CleanFileName);
            blockBlob.DownloadToFile(gg, FileMode.Create);
        }
        catch (Exception e)
        {
            throw e;
        }
        return Request.CreateResponse(HttpStatusCode.OK, fileName + " was downloaded succesfully");
    }
}

As mentioned above, Environment.GetFolderPath(Environment.SpecialFolder.Desktop) wont work on server environment. 如上所述,Environment.GetFolderPath(Environment.SpecialFolder.Desktop)无法在服务器环境上工作。 So you have to try something like: 因此,您必须尝试以下操作:

string gg = Path.Combine(Server.MapPath("~\SomeDirectoryName"), CleanFileName)

Hope this would help. 希望这会有所帮助。

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

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