我有一个运行框架版本.NET 4.5的Asp.Net MVC应用程序,我正在使用VS2017专业版.用户可以上传附件,包括但不限于:
所以我有一个private如下功能:
private string ImageExtension(string path)
{
string noImagePath = HttpContext.Current.Server.MapPath("~/Content/images/Template/No-Image-Available-100x100.jpg");
string fileExtension = System.IO.Path.GetExtension(path);
switch (fileExtension)
{
case ".jpg":
case ".jpeg":
case ".gif":
case ".png":
return path;
default:
return noImagePath;
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,它非常简单,并没有做任何花哨的事情.因为我只在一个地方使用它,所以我考虑使用本地功能的新C#7功能.所以我继续创建我的主要功能如下并添加ImageExtension(string path)到其中.
public void BugInfo(HttpPostedFileBase file)
{
if(file != null && file.ContentLength > 0)
{
string fileName = file.FileName;
string directoryPath = "directory path";
//save path of
string savePath = System.IO.Path.Combine(directoryPath, fileName);
string testString = …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像的扩展方法
public static T ThrowIfObjectIsNull<T>(this T argument) where T : class
{
if (argument == null)
throw new ArgumentNullException(nameof(argument));
return argument;
}
Run Code Online (Sandbox Code Playgroud)
这基本上检查传递的对象是否为空.我想要做的是创建另一个扩展方法,int其中传入的值不是0.所以我继续创建:
public static T ThrowIfZero<T>(this T argument) where T : struct
{
if (argument == 0)
throw new ArgumentOutOfRangeException("some error here");
return argument;
}
Run Code Online (Sandbox Code Playgroud)
当然上面没有编译提示错误:
错误CS0019运算符'=='不能应用于'T'和'int'类型的操作数
有人能指出我如何检查参数值是否正确0?
我目前正在开发一个使用Asp.Net和C#开发的网站.我正在利用Asp.Net Handler允许用户下载文件.我可以下载文件没问题.但是我需要记录哪些文件已成功下载.这部分似乎对我不起作用.例如,如果我单击要下载的文件,然后在浏览器提示上单击取消,我的代码仍会写入日志.我似乎无法弄清楚只有在文件成功下载后才能写入日志.
我的代码如下.
public void ProcessRequest(HttpContext context)
{
string logFilePath = "PathToMyLogFile";
string filePath = Uri.UnescapeDataString(context.Request.QueryString["file"]);
string fileName = Path.GetFileName(filePath);
if (context.Response.IsClientConnected) //Shouldn't this tell me if the client is connected or not?
{
using (var writer = new StreamWriter(logFilePath, true))
{
if (!File.Exists(logFilePath))
{
//Create log file if one does not exist
File.Create(logFilePath);
}
else
{
writer.WriteLine("The following file was downloaded \"{0}\" on {1}", fileName, DateTime.Now.ToString("dd/MM/yyyy") + " at " + DateTime.Now.ToString("HH:mm:ss"));
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的linq查询,如下所示:
var result = (from r in employeeRepo.GetAll()
where r.EmployeeName.Contains(searchString)
|| r.SAMAccountName.Contains(searchString)
orderby r.EmployeeName
select new SelectListItem
{
Text = r.EmployeeName,
Value = r.EmployeeName
});
Run Code Online (Sandbox Code Playgroud)
这个问题是出于一些奇怪的原因,它把我搜索的每个人的记录都记录下来,无论是小写还是大写.即
我会找回正确的记录.然而,当我使用小写字母搜索我自己的名字时,我没有得到任何结果,但如果我使用我的名字的第一个字母作为大写,那么我得到结果.我似乎无法弄清楚为什么这样做.
数据库中的每个名字和姓氏都以大写字母开头.
我正在使用的searchString是:
richard - 我得到了正确的结果waidande - 未找到结果上述两个用户都在数据库中.
我也Entity Framework用来查询Sql Server 2012.
我想实现的标准Id中AspNetUsers,从nvarchar到int.我已经设法让那一方工作了.但是我的问题是当我尝试登录时,我不断从UserManager课堂上收到错误.
我的代码如下:
public class UserManager : UserManager<ApplicationUser, int>
{
public UserManager(IUserStore<ApplicationUser, int> store)
: base(store)
{
}
Run Code Online (Sandbox Code Playgroud)
在我登录的页面上
if (IsValid)
{
// Validate the user password
var manager = Context.GetOwinContext().GetUserManager<UserManager>();
var user = manager.Find(UserName.Text, Password.Text); //This line throws the error
if (user != null)
{
IdentityHelper.SignIn(manager, user, isPersistent: false);
Response.Redirect("~/Home.aspx"); }
else
{
FailureText.Text = "Invalid username or password.";
ErrorMessage.Visible = true;
}
}
Run Code Online (Sandbox Code Playgroud)
我一直得到的错误是System.ArgumentNullException:Value不能为null.参数名称:manager.还有其他人遇到过这个问题吗?在此先感谢您的帮助
堆栈跟踪
[ArgumentNullException: Value …Run Code Online (Sandbox Code Playgroud) 每次传入连接字符串时,是否可以使用实体框架动态创建整个模型(数据库优先)方法?
我尝试了以下方法:
MetaModel model = new MetaModel();
model.RegisterContext(() => new Model1(connectionString),
new ContextConfiguration()
{
ScaffoldAllTables = true
});
Run Code Online (Sandbox Code Playgroud)
但它一直在给我一个错误
System.Web.DynamicData.dll中发生了未处理的"System.ArgumentException"类型异常
附加信息:不支持上下文类型"DbContext.Model1".
更多信息:
我拥有数据库中所有必需的表,我无法控制,如果我需要旧表中的任何新表或列,那么db人员会为我运行相应的脚本.
我正在尝试创建一个通用的DLL EF,我可以使用它在各种应用程序6,包括winforms和mvcWeb应用程序.我想弄清楚我们最好的方法是什么.
我可以混合codefirst和databasefirst在一起?
不会codefirst为我生成我已经拥有的表格吗?
我的每个应用程序都包含1个winforms和1个Web应用程序,并且它们共享数据源,例如
测试1 Windows App&Test 1 MVC App = DB 1
测试2 Windows App&Test 2 MVC App = DB 2
测试3 Windows App&Test 3 MVC App = DB 3
所以我需要传递connectionstring到DBContext.我的实体将如何运作?
如果需要更多信息,请告诉我.
我有一个非常奇怪的场景,我似乎无法弄清楚发生了什么.我有一个用C#编写的Web服务,目标框架是3.5.我有很多类和方法但为了简单起见,我将在问题中只使用两个类.
public class PathNames
{
private string _pathA = "Some Path";
private string _pathB = "Another Path";
public string BaseDirectoryPath
{
get
{
return Path.Combine(_pathA, _pathB);
}
}
}
Run Code Online (Sandbox Code Playgroud)
第二节如下:
public class UserInformation
{
public string UserName { get; set; }
...//more properties
}
Run Code Online (Sandbox Code Playgroud)
上述两个类都是相同的namespace.
Web服务在WebForm应用程序中引用,目标框架为4.0.一切似乎工作正常,我可以在看到UserInformation它时看到课程Object Browser.然而,这个PathNames班似乎并不可见Object Broswer.
有问题的两个源文件都设置Compile在File Propertieswindows中.我有5个类似于窗口中的UserInformation相同设置File Properties,它们只是简单的POCO,只有公共汽车.这些都似乎正在通过,我可以访问它们并在中看到它们Object Browser.由于一些奇怪的原因,我无法PathNames上课.我试图添加一些新的虚拟类,并与类具有相同的问题PathNames.有人可以告诉我,我做错了.
Web服务旧ASMX
正在通过VS添加服务引用创建Web服务客户端
使用VS 2017专业版 …
我对DI很新,所以我的知识并不是最好的.我目前正在开发Asp.Net Core MVC应用程序.
假设我有一个返回a的DLL DataSet(我可以完全控制这个DLL并可以根据需要更改它).
所以DLL中的代码如下所示:
public class DataStore : IDataStore
{
private readonly IConfiguration _configuration;
public DataStore(IConfiguration configuration)
{
_configuration = configuration;
}
public DataSet GetDataBySP(string spName, SqlParameter[] parameters = null)
{
DataSet ds = new DataSet();
using(var con = new SqlConnection(_configuration.GetConnectionString("conStr")))
{
using(var cmd = new SqlCommand(spName, con))
{
cmd.CommandType = CommandType.StoredProcedure;
if (parameters?.Length > 0)
cmd.Parameters.AddRange(parameters);
con.Open();
using (var da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
}
}
}
return ds;
}
}
Run Code Online (Sandbox Code Playgroud)
现在假设我有一个名为class的类Foo,它只包含两个属性,如下所示:
public …Run Code Online (Sandbox Code Playgroud) 我正在使用MVC应用程序.登录由Web API处理.
我面临的问题是,当我点击MVC应用程序中的" 登录"按钮时,API正在生成令牌以及用户信息.但是我无法在视图中显示用户信息.
以下代码位于AccountControllerMVC App中
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model)
{
string token = Token.GetToken(Token.GRANT_TYPE, model.Email, model.Password, Token.CLIENT_ID, Token.CLIENT_SECRET);
//token has the appropriate data if I debug it
if (!string.IsNullOrEmpty(token))
return RedirectToAction("Index", "Home");
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
然后在视图中我有
@if (Request.IsAuthenticated)
{
<p>@User.Identity.GetUserName()</p> //this line is always blank
}
Run Code Online (Sandbox Code Playgroud)
该API看起来是这样的
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin");
if (allowedOrigin == …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc asp.net-web-api asp.net-mvc-5 asp.net-identity
我遇到以下代码的问题:
string latString = "50.09445";
float lat = Convert.ToSingle(latString);
Run Code Online (Sandbox Code Playgroud)
第二个命令抛出FormatException异常.我知道问题是我正在使用的文化设置(cs-CZ)使用逗号作为小数点分隔符,而这个字符串包含小数点.
是否有一些简单的方法可以"忽略"文化设置并始终使用小数点进行转换?或者我应该先通过检查字符串并用小数点替换逗号来避免问题?
c# ×10
asp.net ×5
asp.net-mvc ×3
.net ×1
.net-core ×1
c#-7.0 ×1
linq ×1
owin ×1
sql-server ×1
web-services ×1
winforms ×1