对待AbsoluteUri C#

时间:2017-04-07 12:45:06

标签: c# xml url

我试图找出从URL项目中处理MVC的最佳方式。

正如您将看到我正在阅读XML文件以区分女性客户端进入IIS

我的问题是,有时我得到空URl,有时客户端会重定向到错误的路径。我正在使用node.Value.Contains(ClientUrl).FirstOrDefault()。所以每当发生这种情况时,我需要调整XML,就像添加/login#等一样。 由于所有URL's都是类似的,我希望将每个客户的网址/路径视为使用node.Value.Equals(ClientUrl)

谢谢大家。

控制器

    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();
    }

阅读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示例

    <?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>

1 个答案:

答案 0 :(得分:0)

我决定通过使用数组来设置它们一样简单。

           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("/"));              
            }