csharp C#或ASP.Net Utility.cs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#或ASP.Net Utility.cs相关的知识,希望对你有一定的参考价值。


// GET PROJECT ROOT DIRECTORY AS String (for Web/ASP.Net)
public static readonly string ProjectRootDirectory = new DirectoryInfo(
				Path.GetDirectoryName(Uri.UnescapeDataString(
					new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path))).Parent.FullName;
					

// GET PROJECT ROOT DIRECTORY AS String (for C# Console or WindowsForm)					
public static readonly string ProjectRootDirectory = System.IO.Directory.GetCurrentDirectory();



// MIGHT BE NEEDED IN HomeController.cs (later.........)
//var model = _menuConfig.GetByRole(Settings.RoleId).Where(m => m.Active); //ACTIVE
//var model = _menuConfig.GetMenu(Settings.RoleId, Settings.UserId).Where(m => m.Active); //COMMENTED
List<MenuConfig> model = new List<MenuConfig>();
if (_solutionConfig.IsUserWisePrivileges().ToLower() == "yes")
{
    var modelTemp = _menuConfig.GetMenu(Settings.RoleId, Settings.UserId).Where(m => m.Active).ToList();                
    var parentMenus = modelTemp.Where(x => x.Parent == null);
    foreach (var p in parentMenus)
    {
        foreach (var item in modelTemp)
        {
            if (p.Id == item.Id && modelTemp.Count(x => x.Id == p.Id) > 1)
            {
                modelTemp.Remove(p);
            }
        }
    }
    model = (List<MenuConfig>) modelTemp;
}
else
{
    model = (List<MenuConfig>) _menuConfig.GetByRole(Settings.RoleId).Where(m => m.Active);
}







void Session_Start(object sender, EventArgs e)
{
    //int maximumAllowedSessions = 0;
    //string connectionSring = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    //SqlConnection connection = new SqlConnection(connectionSring);
    //string query = "SELECT ValueField FROM SolutionConfig WHERE DisplayField = 'MaximumConcurrentConnection' AND FunctionName = 'MaximumConcurrentConnection'";
    //SqlCommand command = new SqlCommand(query, connection);
    //connection.Open();
    //SqlDataReader reader = command.ExecuteReader();
    //while (reader.Read())
    //{
    //    maximumAllowedSessions = int.Parse(reader["ValueField"].ToString());
    //}
    //reader.Close();
    //connection.Close();


    //int activeSessions = (int)Application["ActiveSessions"] + 1;
    //int allowedSessions = maximumAllowedSessions; // retrieve the threshold here instead
    //Application["ActiveSessions"] = activeSessions;
    //if (activeSessions > allowedSessions)
    //{
    //    Application.Lock();
    //    Response.RedirectToRoute("Default");
    //}
}

protected void Session_End(object sender, EventArgs e)
{
    //Application["ActiveSessions"] = (int)Application["ActiveSessions"] - 1;
}



// TUPLE EXAMPLE
public void Group()
{
    List<Tuple<int, int>> SongRelations = new List<Tuple<int, int>>();

    SongRelations.Add(new Tuple<int, int>(1, 1));
    SongRelations.Add(new Tuple<int, int>(1, 4));
    SongRelations.Add(new Tuple<int, int>(1, 12));
    SongRelations.Add(new Tuple<int, int>(2, 95));

    var list = SongRelations.GroupBy(s => s.Item1)
                            .Select(r => new SongsForUser()
                            {
                                UserId = r.Key,
                                Songs = r.Select(t => t.Item2).ToList(),
                            });
}


// Upload file
[HttpPost]
[UserAuthorize]
public void UploadFile()
{
    try
    {

        foreach (string file in Request.Files)
        {
            var fileContent = Request.Files[file];
            if (fileContent != null && fileContent.ContentLength > 0)
            {
                fileContent.SaveAs(Server.MapPath("~/Uploads/KYCDocuments/") + Path.GetFileName(fileContent.FileName));
            }
        }
    }
    catch (Exception)
    {
        Response.StatusCode = (int)HttpStatusCode.BadRequest;

    }
}


// Download File
[UserAuthorize]
public void Download(string val)
{

    var filePath = Server.MapPath("~/Uploads/KYCDocuments/") + Path.GetFileName(val);
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);

    Response.Clear();
    Response.AppendHeader("Content-Disposition", "inline; filename=" + Path.GetFileName(filePath));
    Response.ContentType = "application/text/plain";
    Response.BinaryWrite(bytes);
    //Response.Flush();
    Response.End();

}





// GetObjectPropertiesWithValue as String 
public static string GetObjectPropertiesWithValue(object target)
{
    var properties = from property in target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
    select new
    {
        Name = property.Name,
        Value = property.GetValue(target, null)
    };

    var builder = new StringBuilder();
    foreach (var property in properties)
    {
        builder
            .Append(property.Name)
            .Append(" = ")
            .Append(property.Value)
            .AppendLine();
    }
    return builder.ToString();
}


以上是关于csharp C#或ASP.Net Utility.cs的主要内容,如果未能解决你的问题,请参考以下文章

csharp C#:JsonResult替代ASP.NET MVC。

ASP.NET Aries JSAPI 文档说明:AR.Utility

ASP.NET Aries JSAPI 文档说明:AR.Utility

csharp 一些常见的连接字符串(在Asp.Net和C#应用程序中非常常用)

csharp 将Castle Windsor依赖项注入ASP.NET Web API过滤器C#

csharp ASP.NET MVC ActionFilter允许或拒绝具有可选子网掩码的IPv4地址。用于AppHarbor或任何其他服务器解决方案