2017-04-07 10 views
0

私はをMVCプロジェクトから治療する最良の方法を理解しようとしています。治療するAbsoluteUri C#

あなたは私がクライアントを区別するためにXMLファイルを読んでいることが分かるので、どこにIISに入ります。

私の問題は、ときどき私は空のURlを取得することがあります。クライアントが間違ったパスにリダイレクトされることがあります。私はnode.Value.Contains(ClientUrl).FirstOrDefault()を使っています。それが起こるときはいつでも、私は/login#などを追加するようにそれにXMLを適応させる必要があります。 すべてURL'sと同様に私はURL /パスを各クライアントに扱いたいと思いますnode.Value.Equals(ClientUrl)

ありがとうございます。

<?xml version="1.0" encoding="utf-8"?> 
    <ClientConfig> 
     <SysWeb> 
     <client url="http://192.168.2.31/sysweb/client1"> 
      <wsdl>http://192.168.2.25/wssysweb/sysweb.dll/soap/ISysWeb</wsdl> 
      <clientId>001</clientId> 
      <urlAppMobile>http://192.168.2.31/syswebmobile/AppClient1</urlAppMobile> 
      <urlAppDesktop>http://192.168.2.31/sysweb/AppClient1</urlAppDesktop> 
     </client> 
     <client url="http://192.168.2.31/sysweb/client1/login#"> 
      <wsdl>http://192.168.2.25/wssysweb/sysweb.dll/soap/ISysWeb</wsdl> 
      <clientId>001</clientId> 
      <urlAppMobile>http://192.168.2.31/syswebmobile/AppClient1</urlAppMobile> 
      <urlAppDesktop>http://192.168.2.31/sysweb/AppClient1</urlAppDesktop> 
     </client>  
     <client url="http://192.168.2.31/sysweb/client2"> 
      <wsdl>http://192.168.2.25/wssyweb/syspweb.dll/soap/ISysWeb</wsdl> 
      <clientId>002</clientId> 
      <urlAppMobile>http://192.168.2.31/syswebmobile/AppClient2</urlAppMobile> 
      <urlAppDesktop>http://192.168.2.31/sysweb/AppClient2</urlAppDesktop> 
     </client>  
     <client url="http://192.168.2.31/sysweb/client3"> 
      <wsdl>http://192.168.2.25/wssyweb/syspweb.dll/soap/ISysWeb</wsdl> 
      <clientId>003</clientId> 
      <urlAppMobile>http://192.168.2.31/syswebmobile/AppClient3</urlAppMobile> 
      <urlAppDesktop>http://192.168.2.31/sysweb/AppClient3</urlAppDesktop> 
     </client>  
     </SysWeb> 
    </ClientConfig> 

答えて

0

XML

private static void readXMLConfig(string ClientUrl) 
    { 
     try 
     { 

      XmlDocument docX = new XmlDocument(); 

      string sysWebPath = AppDomain.CurrentDomain.BaseDirectory; 

      docX.Load(WebPath + "/SysWebConfig.xml"); 

      XElement xml = docX.ToXElement(); 
      var config = (from nodes in xml.Descendants("client") 
           from node in nodes.Attributes("url") 
           where node.Value.Contains(ClientUrl) 
           select nodes).FirstOrDefault(); 

      if (config != null) 
      { 
       _client = int.Parse(config.Element("clientId").Value); 
       _wsdl = config.Element("wsdl").Value; 
       _urlAppMobile = config.Element("urlAppMobile").Value; 
       _urlAppDesktop = config.Element("urlAppDesktop").Value; 
      } 

     } 
     catch (Exception) 
     { 
      throw new Exception('error reading xml file'); 
     } 
    } 

XMLサンプルを読む

コントローラ

public ActionResult Index(string returnUrl) 
    { 
     try 
     { 
      Response.ClearContent(); 
      Response.ClearHeaders(); 
      Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); 
      Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      Response.Cache.SetNoStore();    

      var useragent = Request.UserAgent; 
      string UrlDoCliente = Request.Url.AbsoluteUri; 
      SysConfig _SysConfig = new SysConfig(ClientUrl); 
      var isAndroid = false; 
      int Id = SyscoopWebConfig.Empresa; 
      if (useragent != null) 
      { 
       if (useragent.ToLower().Contains("Android".ToLower())) 
       { 
        isAndroid = true; 
       } 
       else 
       { 
        isAndroid = Request.Browser.IsMobileDevice; 
       } 
      } 

      if (isAndroid) 
      { 
       string url = SysWebConfig.urlAppMobile; 
       if (url != "") 
       { 
        return Redirect(url); 
       } 
      } 

      if (!Request.IsAuthenticated) 
      { 
       Session["sesParam"] = null; 
       return Redirect(SysWebConfig.urlAppDesktop + "/login#"); 
      } 
     } 
     catch (Exception e) 
     { 
      ViewBag.message = e.Message.ToString() + ". Url wsdl: " + SysWebConfig.Wsdl; 
     } 

     return View(); 
    } 

はI'veはすべて同じ、それらを設定するために、配列を使用することにより、簡単な行くことにしました。

  string ClientUrl = Request.Url.AbsoluteUri;    

      string[] vUrlArray = ClientUrl.Split('/'); 

      if (vUrlArray.Count() >= 6) 
      { 
       ClientUrl = ""; 
       for (int i = 0; i <= 4; i++) 
       { 
        ClientUrl = ClientUrl + vUrlArray[i] + "/"; 

       } 
       ClientUrl = ClientUrl.Substring(0, UrlDoCliente.LastIndexOf("/"));    
      }