سلام خسته نباشید
من یه سایت دوزبانه دارم که در فایل global.asax تمام url های سایت رو به url هایی که زبان سایت رو مشخص میکنن(/lang/controller/action) ، ریدایرکت کردم :
protected void Application_BeginRequest() { var routes = RouteTable.Routes; var httpContext = Request.RequestContext.HttpContext; if (httpContext == null) return; var routeData = routes.GetRouteData(httpContext); var lang = routeData.Values["lang"]; if (lang != null && !string.IsNullOrWhiteSpace(lang.ToString()) && (lang.ToString() == "fa" || lang.ToString() == "en")) { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang.ToString()); Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang.ToString()); HttpCookie cooki = new HttpCookie("Language"); cooki.Value = lang.ToString(); cooki.Expires.AddYears(1); Response.Cookies.Add(cooki); } else { HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"]; if (cookie != null && cookie.Value != null && (cookie.Value == "fa" || cookie.Value == "en")) { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie.Value); Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie.Value); var ur = string.Format("{0}://{1}{2}{3}{4}", System.Web.HttpContext.Current.Request.Url.Scheme, System.Web.HttpContext.Current.Request.Url.Host, System.Web.HttpContext.Current.Request.Url.Port == 80 ? string.Empty : ":" + System.Web.HttpContext.Current.Request.Url.Port, "/" + cookie.Value, System.Web.HttpContext.Current.Request.RawUrl); Response.RedirectPermanent(ur); } else { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fa"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("fa"); HttpCookie cook = new HttpCookie("Language"); cook.Value = "fa"; cook.Expires.AddYears(1); Response.Cookies.Add(cook); var ur = string.Format("{0}://{1}{2}{3}{4}", System.Web.HttpContext.Current.Request.Url.Scheme, System.Web.HttpContext.Current.Request.Url.Host, System.Web.HttpContext.Current.Request.Url.Port == 80 ? string.Empty : ":" + System.Web.HttpContext.Current.Request.Url.Port, "/fa", System.Web.HttpContext.Current.Request.RawUrl); Response.RedirectPermanent(ur); } } }
حالا موقع آپلود تصویر با ckeditor ، مشکل پیش میاد و تصاویر آپلود نمیشن ، در فایل config.js مربوط به ckeditor هم آدرس کنترلر و اکشن رو به صورت زیر دادم :
/controller/action
کد های مربوط به آپلود تصویر در کنترلر به صورت زیر است:
public class CKEditorController : Controller { // GET: CKEditor [HttpPost] public ActionResult UploadImage(HttpPostedFileBase upload, string CKEditorFuncNum, string CKEditor, string langCode) { string vImagePath = String.Empty; string vMessage = String.Empty; string vFilePath = String.Empty; string vOutput = String.Empty; try { if (upload != null && upload.ContentLength > 0) { var vFileName = DateTime.Now.ToString("yyyyMMdd-HHMMssff") + Path.GetExtension(upload.FileName).ToLower(); var vFolderPath = Server.MapPath("/Content/Upload/"); if (!Directory.Exists(vFolderPath)) { Directory.CreateDirectory(vFolderPath); } vFilePath = Path.Combine(vFolderPath, vFileName); upload.SaveAs(vFilePath); vImagePath = Url.Content("/Content/Upload/" + vFileName); vMessage = "تصویر با موفقیت ذخیره شد"; } } catch { vMessage = "خطا هنگام آپلود!"; } vOutput = @"<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + vImagePath + "\", \"" + vMessage + "\");</script></body></html>"; return Content(vOutput); } }
برای routing هم از کد های زیر استفاده کردم :
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.Add("BlogDetails", new SeoFriendlyRoute("{lang}/Home/BlogDetail/{id}", new RouteValueDictionary(new { controller = "Home", action = "BlogDetail" }), new MvcRouteHandler())); routes.Add("RoomDetails", new SeoFriendlyRoute("{lang}/Home/Room/{id}", new RouteValueDictionary(new { controller = "Home", action = "Room" }), new MvcRouteHandler())); routes.Add("BlogCategories", new SeoFriendlyRoute("{lang}/Home/Blog/{id}", new RouteValueDictionary(new { controller = "Home", action = "Blog" }), new MvcRouteHandler())); // Localization route - it will be used as a route of the first priority routes.MapRoute( name: "DefaultLocalized", url: "{lang}/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }
خواستم کدهام رو trace کنم تا مشکل رو پیدا کنم برای همین در کنترلر و اکشن مربوطه break point گذاشتم ولی ظاهرا اصلا وارد کنترلر و اکشن نمیشه ، فکر کنم یه جایی تو همون
Application_BeginRequest یا routing گیر میکنه،
لطفا راهنمایی کنین، خیلی ممنون
لطفا تب Console یا Network مروگر را بررسی کنید و ببینید که آیا خطایی وجود دارد؟
وقتی تب console رو باز میکنم و با ckeditor یک تصویر انتخاب میکنم و انتقال به سرور رو میزنم فقط خطای زیر رو میده که فکر نکنم از اون باشه :
Password fields present on an insecure (http://) iframe. This is a security risk that allows user login credentials to be stolen
در تب network هم ظاهرا ریدایرکت 301 و 302 برای این درخواست اتفاق افتاده که باعث شده در خواست post به get تغییر کنه ، محل مشخص شده در تصویر زیر رو میگم ،اکشن uploadimage
مشکل از کجاست؟آیا ریدایرکت اضافی دارم؟ لطفا راهنمایی کنید.ممنون.
احساس میکنم عملیات Redirect شدن در همان جایی که در حال تنظیم کردن کوکی هستید رخ میده در متد Application_BeginRequest.
راستش من چند زبانگی سایت رو با کوکی پیاده کردم ، مطابق آموزش جناب مهندس مدائنی ، بعد برای اینکه برای سئو هم خوب باشه اومدم آدرس هام رو با کمک همون ریدایرکت هایی که در Application_BeginRequest اتفاق می افته به شکل زیر درآوردم:
/language/controller/action
در قسمت Application_BeginRequest کاری که میکنه اینه که اول بررسی میکنه که آیا url درخواست شده مطابق فرمت بالا language رو داره یانه، اگر زبان رو نداشت، زبان سایت رو از کوکی میخونه و به url اضافه میکنه. در غیر اینصورت اگر زبان در url مشخص شده باشه همون زبان رو از URL میخونه و کوکی رو هم ست میکنه(جهت محکم کاری)،
حالا آدرسی که در CKEDITOR برای آپلود تصویر تنظیم شده /Controller/Uploadimage هست که بعد از ریدایرکت به شکل /language/controller/uploadimage در میاد.
حالا این ریدایرکت باعث شده که تصویری که توسط ckeditor قراره آپلود بشه وسط راه گم میشه ، اصلا به اکشن مربوطه نمیرسه، نمیدونم کجا میره ...؟
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)