相关疑难解决方法(0)

为多语言ASP.NET MVC Web应用程序设置CurrentCulture的最佳位置

对于多语言ASP.NET MVC 3 Web应用程序,我正在确定Thread.CurrentThread.CurrentCultureThread.CurrentThread.CurrentUICulture控制器工厂,如下所示:

public class MyControllerFactory : DefaultControllerFactory {

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) {

        //Get the {language} parameter in the RouteData
        string UILanguage;
        if (requestContext.RouteData.Values["language"] == null)
            UILanguage = "tr";
        else
            UILanguage = requestContext.RouteData.Values["language"].ToString();

        //Get the culture info of the language code
        CultureInfo culture = CultureInfo.CreateSpecificCulture(UILanguage);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        return base.GetControllerInstance(requestContext, controllerType);
    }

}
Run Code Online (Sandbox Code Playgroud)

上面的代码已经快一年了!所以,我愿意接受建议.

我在Global.asax文件上注册这个,如:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
Run Code Online (Sandbox Code Playgroud)

这样做很好,但我不确定这是否是做这种行为的最佳做法和最佳场所.

我没有挖掘主要角色,ControllerFactory我无法将其与之比较ActionFilterAttribute.

你怎么看待这种行动的最佳地点?

asp.net-mvc action-filter controller-factory asp.net-mvc-3

41
推荐指数
2
解决办法
3万
查看次数

ASP.NET MVC(异步)CurrentCulture不在Controller和View之间共享

我有一个针对.NET Framework 4.7.1的ASP.NET MVC 4应用程序,如果操作包含异步调用,则存在Controller和View之间不共享文化的问题.

我正在引用NuGet包Microsoft.AspNet.Mvc5.2.3(可以在5.2.4中复制).

这是Controller中的代码:

public class CulturesTestController : Controller
{
    public async Task<ActionResult> Index(string value)
    {
        Thread.CurrentThread.CurrentCulture = 
            CultureInfo.GetCultureInfo("fi-FI");
        Thread.CurrentThread.CurrentUICulture = 
            CultureInfo.GetCultureInfo("fi-FI");
        var model = new CulturesContainer
        {
            CurrentCulture = Thread.CurrentThread.CurrentCulture,
            CurrentUICulture = Thread.CurrentThread.CurrentUICulture,
            CurrentThreadId = Thread.CurrentThread.ManagedThreadId
        };
        Log.Write(Level.Info, "CurrentUICulture - Before Await - " +
                              "CurrentCulture: " +
                              $"{Thread.CurrentThread.CurrentCulture}, " +
                              "CurrentUICulture: "
                              ${Thread.CurrentThread.CurrentUICulture} -> "+
                              "ThreadId: " + 
                              $"{Thread.CurrentThread.ManagedThreadId}");

        await GetAwait();
        Log.Write(Level.Info, "CurrentUICulture - After Await - " +
                              "CurrentCulture: " + 
                              $"{Thread.CurrentThread.CurrentCulture}, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc cultureinfo currentuiculture async-await

10
推荐指数
1
解决办法
999
查看次数

为什么我的主机(softsyshosting.com)不支持BeginRequest和EndRequest事件处理程序?

我听说过Softsys Hosting的好东西,所以我决定将我的ASP.NET MVC解决方案移交给他们.但它不会在他们身上运行.我能够向我的BeginRequest事件处理程序查明问题.如果我有他们我会得到一个错误.这是我的代码.

protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);
    this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
    this.EndRequest += new EventHandler(MvcApplication_EndRequest);
} 

void MvcApplication_EndRequest(object sender, EventArgs e) 
{
}

void MvcApplication_BeginRequest(object sender, EventArgs e) 
{
}
Run Code Online (Sandbox Code Playgroud)

我可以通过创建默认的ASP.NET MVC应用程序并添加上面的代码来重现该问题.奇怪的是这个代码在我的旧主机上运行良好,它只在我的新(共享)主机上崩溃.如果我的代码中有这些事件处理程序,我会收到此错误:

'/'应用程序中的服务器错误.你调用的对象是空的.描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例.

源错误:在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.

堆栈跟踪:

[NullReferenceException:对象引用未设置为对象的实例.] System.Web.PipelineModuleStepContainer.GetStepArray(RequestNotification notification,Boolean isPostEvent)+27 System.Web.PipelineModuleStepContainer.GetEventCount(RequestNotification notification,Boolean isPostEvent)+11 System.Web .PipelineStepManager.ResumeSteps(异常错误)+205 System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context,AsyncCallback cb)+91 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext context)+514

我尝试使用Softsys对此进行故障排除,但它们不是很有用,基本上它们只是确认我在管理控制面板中打开了"ASP.NET管道(MVC)"功能.

有人可以:

  1. 告诉我,如果我编码错了
  2. 告诉我一个解决方法
  3. 向我解释为什么这个错误发生在一个主机而不是另一个主机上.

asp.net asp.net-mvc

9
推荐指数
2
解决办法
4135
查看次数