Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net mvc中每天执行一次操作_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 在asp.net mvc中每天执行一次操作

C# 在asp.net mvc中每天执行一次操作,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个asp.NETMVC5网站,它从api获取一些图像文件,并将它们存储在图像文件夹中 public async Task<bool> getPhoto64(UserTest user) { try { if (!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png")

我有一个asp.NETMVC5网站,它从api获取一些图像文件,并将它们存储在图像文件夹中

public async Task<bool> getPhoto64(UserTest user)
    {
        try
        {
            if (!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png"))))
                using (var client = new HttpClient())
                {
                    string baseUrl = ConfigurationManager.AppSettings["BaseUrl"];
                    client.BaseAddress = new Uri(baseUrl);
                    client.DefaultRequestHeaders.Accept.Clear();


                    HttpResponseMessage response = await client.GetAsync("user/image?userIid=" + user.IID);
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        Stream st = await response.Content.ReadAsStreamAsync();

                        byte[] bytes = new byte[(int)st.Length];
                        st.Read(bytes, 0, (int)st.Length);
                        if (bytes != null)
                        {

                            if (!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png"))))
                                File.WriteAllBytes(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/images/" + user.IID + ".png")), bytes);


                        }
                    }
                }
        }
        catch (Exception e)
        {
            logger.Error(e);
            return false;
        }
        return true;

    }
公共异步任务getPhoto64(UserTest用户)
{
尝试
{
如果(!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath(“~/images/”+user.IID+“.png”)))
使用(var client=new HttpClient())
{
字符串baseUrl=ConfigurationManager.AppSettings[“baseUrl”];
client.BaseAddress=新Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response=wait client.GetAsync(“用户/image?userIid=“+user.IID”);
if(response.StatusCode==System.Net.HttpStatusCode.OK)
{
Stream st=wait response.Content.ReadAsStreamAsync();
字节[]字节=新字节[(int)st.Length];
st.Read(字节,0,(int)st.Length);
如果(字节数!=null)
{
如果(!File.Exists(Path.Combine(System.Web.HttpContext.Current.Server.MapPath(“~/images/”+user.IID+“.png”)))
File.writealBytes(Path.Combine(System.Web.HttpContext.Current.Server.MapPath(“~/images/”+user.IID+“.png”)),字节);
}
}
}
}
捕获(例外e)
{
错误(e);
返回false;
}
返回true;
}

现在,当用户连接到网站时,我会这样做,但我想知道是否有一种方法可以让网站自动触发此方法,比如说每天00:00一次。

你可以使用“Windows任务调度器”来实现此目的。

我不认为这与MVC有关,请看一看,或者Scott Hanselman去年在ASP.NET中提供了关于计划任务的选项。这是服务应用程序的作业使用窗口执行计划任务计划程序可以使用午夜运行一次的控制台应用程序进行管理。