1、asp.net MVC在web.config里配置sessionstate,可以让服务端获取网址中的sessionid或者获取cookie里的sess
把状态写在cookie中,先取网址,再取cookie
2、MVC中怎么获取DisplayName-CSDN论坛
public partial class TempXZF
{
public int id { get; set; }
public string XZF_PCH { get; set; }
public string XZF_Name { get; set; }
public string XZF_ZCH { get; set; }
public string XZF_SHXYM { get; set; }
public string XZF_SCDZ { get; set; }
public string XZF_FR { get; set; }
public string XZF_SPZL { get; set; }
}
但因为是EF DB frist自动生成的,所以每次更新的时候,给里面定义的DisplayName就会被刷掉,参考了网上的方法后,
使用MetadataType
[MetadataType(typeof(TempXZFData))]
public partial class TempXZF
{
class TempXZFData
{
public int id { get; set; }
[DisplayName("批次号")]
public string XZF_PCH { get; set; }
[DisplayName("企业名称")]
public string XZF_Name { get; set; }
[DisplayName("工商注册号")]
public string XZF_ZCH { get; set; }
[DisplayName("社会信用代码")]
public string XZF_SHXYM { get; set; }
[DisplayName("生产地址")]
public string XZF_SCDZ { get; set; }
[DisplayName("法定代表人")]
public string XZF_FR { get; set; }
[DisplayName("食品种类")]
public string XZF_SPZL { get; set; }
}
}
在前台可以获取到DisplayName了,比如
@Html.LabelFor(model => model.XZF_PCH)
显示的就是批次号,但是在后台想要获取每个DisplayName要怎么获得?
比如获取XZF_PCH的DisplayName是批次号,
XZF_Name的DisplayName是企业名称这样子
3、ASP.NET(C#)怎么获得当前域名?
HttpContext.Current.Request.Url.Host.ToString()
4、如何在MVC View中获取ViewName,ActionName,ControllerName
网上搜了很多资料,在View中都是关于获取Controller及Action的方法,却没找到获取ViewName的方法。经过不懈努力,终于想到一个实现方式,即用截取文件名来获得。
初学者可能会问,action的名字和view的名字不是一样吗?
这可不一定,所以,还是用文件名的方式比较保险。
先把获取Controller、Action、ViewName的方式全部列出来:
1、获取Controller
string controllerName = 
ViewContext.RouteData.Values["controller"].ToString();
2、获取Action
string actionName = ViewContext.RouteData.Values["action"].ToString();
3、获取ViewName
public static string CurrentViewName(IView view)
{
if (view is BuildManagerCompiledView)
{
string viewName = 
((BuildManagerCompiledView)view).ViewPath;
viewName = 
viewName.Substring(viewName.LastIndexOf('/'));
viewName = 
Path.GetFileNameWithoutExtension(viewName);
return viewName;
}
return 
string.Empty;
}
顺便提一句,ViewContext.RouteData.Values中有controller和action,没有view,那还有什么呢?
经过跟踪发现,就这两个,没其他的了。
5、asp.net mvc获取访问者IP根据IP获取城市地址跳转到相应的页面 求大神!
给你一个获取IP的代码,然后根据获取的地区名去数据库匹配对应的城市或二级域名即可。
 
 #region 获取IP
        /// <summary>
        /// 客户端ip(访问用户)
        /// </summary>
        public static string GetUserIp
        {
            get
            {
                string realRemoteIP = "";
                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                {
                    realRemoteIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',')[0];
                }
                if (string.IsNullOrEmpty(realRemoteIP))
                {
                    realRemoteIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                if (string.IsNullOrEmpty(realRemoteIP))
                {
                    realRemoteIP = System.Web.HttpContext.Current.Request.UserHostAddress;
                }
                return realRemoteIP;
            }
        }
6、C# mvc 获取当前请求的视图名称
<script type="text/javascript">
    $(function () {
        var strUrl = window.location.href;
        var arrUrl = strUrl.split("/");
        var strPage = arrUrl[arrUrl.length - 1];
        alert(strPage);
    });
</script>
7、Spring mvc怎么获取当前应用的url地址
action类:
public class IndexController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
Map<String ,Object> model=new HashMap<String,Object>();
String contextpath;
contextpath = request.getScheme() +"://" + request.getServerName()  + ":" +request.getServerPort() +request.getContextPath();
model.put("contextpath",contextpath);
return new ModelAndView("index",model);
}
如果调用IndexController 的http url是http://127.0.0.1:8080/test/index.do,以上request对象的各方法得到的结果分别是:
request.getScheme() :http
request.getServerName()  :127.0.0.1
request.getServerPort() :8080
request.getContextPath():/test
分别request还有一个有用的方法,request.getServletPath,获取的结果是:index.do,一般用不到。
8、asp.net mvc怎么从地址栏取得登录的id
在你跳转链接的时候传一个参数,而你链接的action对应Controller里的一个action,在action中放入一个 参数Id,此时你在这个action中就可以应用这个参数Id了
如 <a href="/Home/Index/id=3"></a>  对应的HomeController中的Index方法是
     public ActionResult Index(int id)
        {
             ViewData["id"] = id;
               return View();
        }
a标签中的就传到地址栏上了,此时就可以获取ID的值了