سلام برای ذخیره عکس پروفایل ، من با استفاده از این آموزش رفتم ولی به Get<ApplicationDbContext>() از برنامه ارور میده و نمیزاره بیلد کنم پروژه رو
https://barnamenevisan.org/Articles/Article5093.html
var bdUsers = HttpContext.GetOwinContext().Get<ApplicationDbContext>();
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } static ApplicationDbContext() { // Set the database intializer which is run once during application start // This seeds the database with admin user credentials and admin role Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer()); } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } }
لطفا متن خطا را قرار بدید.
CS7036 C# There is no argument given that corresponds to the required formal parameter 'key' of 'IOwinContext.Get<T>(string)'
بسته زیر را نصب کنید:
Microsoft.AspNet.Identity.Owin
نصب هست در پروژه
پروژه رو دانلود و تست کردم خطایی مشاهده نشد.
محتوای فایل packge.config را قرار داده و همچنین بگویید نسخه vs شما چیست ؟
VS 2015
بله پروژه مشکل ندارد ولی کد که در پروژه هست در پروژه من خطا میگیره
البته در همون پروژه این قسمت در identity.models تفاوت داره و static ApplicationDbContext() خالی هست ولی در پروژه خودم به صورت زیر هست :
static ApplicationDbContext() { // Set the database intializer which is run once during application start // This seeds the database with admin user credentials and admin role Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer()); }
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Antlr" version="3.4.1.9004" targetFramework="net452" /> <package id="bootstrap" version="3.3.7" targetFramework="net452" /> <package id="EntityFramework" version="6.1.3" targetFramework="net452" /> <package id="jQuery" version="1.10.2" targetFramework="net452" /> <package id="jQuery.Validation" version="1.11.1" targetFramework="net452" /> <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net452" /> <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.1.0-alpha1" targetFramework="net452" /> <package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net452" /> <package id="Microsoft.AspNet.Identity.Samples" version="2.1.0-alpha1" targetFramework="net452" /> <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" /> <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" /> <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" /> <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" /> <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" /> <package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.2.3" targetFramework="net452" /> <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net452" /> <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" /> <package id="Microsoft.Owin" version="2.1.0" targetFramework="net452" /> <package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net452" /> <package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net452" /> <package id="Microsoft.Owin.Security.Cookies" version="2.1.0" targetFramework="net452" /> <package id="Microsoft.Owin.Security.Facebook" version="2.1.0" targetFramework="net452" /> <package id="Microsoft.Owin.Security.Google" version="2.1.0" targetFramework="net452" /> <package id="Microsoft.Owin.Security.MicrosoftAccount" version="2.1.0" targetFramework="net452" /> <package id="Microsoft.Owin.Security.OAuth" version="2.1.0" targetFramework="net452" /> <package id="Microsoft.Owin.Security.Twitter" version="2.1.0" targetFramework="net452" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" /> <package id="Modernizr" version="2.6.2" targetFramework="net452" /> <package id="Newtonsoft.Json" version="5.0.6" targetFramework="net452" /> <package id="Owin" version="1.0" targetFramework="net452" /> <package id="Respond" version="1.2.0" targetFramework="net452" /> <package id="SweetAlert" version="1.0.0" targetFramework="net452" /> <package id="WebGrease" version="1.5.2" targetFramework="net452" /> </packages>
بسته ها nuget پروژتون رو بروزرسانی کنید
همه package هارو بروزرسانی کردم ولی باز خطا میده
راهی دیگه نداره ؟
متد Get و GetOwinContext در IntelliSense قابل مشاهده می باشد ؟
ممنون درست شد
یه سوال خط زیر دربرنامه چه چیزی رو چک میکند در UserPhotos ?
چون عکس رو که آپلود میکنم بعد وارد این قسمت که میشه وارد if نمیشه و null رو به جای عکس ثبت میکنه . if چه چیزی رو چک میکنه ؟
if (Request.Files.Count > 0) { HttpPostedFileBase poImgFile = Request.Files["UserPhoto"]; using (var binary = new BinaryReader(poImgFile.InputStream)) { imageData = binary.ReadBytes(poImgFile.ContentLength); } }
چگونه مشکل شما حل شد لطفا مطرح کنید.
کدی که قرار دادید ابتدا چک می کند که آیا فایلی به action ارسال شده است یا خیر برای همین تعداد فایل های ارسالی چک کرده اگر بزرگ تر 0 بود بقیه کار هارو انجام می دهد.
package که آپدیت کردم بعد گفتم شاید مشکل namespace هست Ctrl + . که زدم name space رو اضافه نکرد خودم using Microsoft.AspNet.Identity.Owin; رو بالا اضافه کردم حل شد.ممنون
مقدار null کلا میفرسته برا کنترلر .
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken() <h4>Create a new account.</h4> <hr /> @Html.ValidationSummary("", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.UserPhoto, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> <input type="file" name="UserPhoto" id="fileUpload" accept=".png,.jpg,.jpeg,.gif,.tif" /> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" class="btn btn-default" value="Register" /> </div> </div>
public async Task<ActionResult> Register([Bind(Exclude = "UserPhoto")]RegisterViewModel model) { if (ModelState.IsValid) { byte[] imageData = null; if (Request.Files.Count > 0) { HttpPostedFileBase poImgFile = Request.Files["UserPhoto"]; using (var binary = new BinaryReader(poImgFile.InputStream)) { imageData = binary.ReadBytes(poImgFile.ContentLength); } } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; user.UserPhoto = imageData; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); ViewBag.Link = callbackUrl; return View("DisplayEmail"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)