Reputation: 986
I want to grab the IP Address of the client machine, then assign a name to it. Say if it was 192.168.7.12 then that workstation would have the name "Head Board Assembly" then pass it back to the Layout view. The assigning names bit isnt the issue, however, i want to pass the end value to the main layout and not repeat the code as below. Ignore the Server IP method, its in early stages on local host.
HomeController.cs
public class HomeController : Controller
{
[Authorize]
public ActionResult Index()
{
ViewBag.HtmlStr = "Workstation name: " + IP();
return View();
}
[Authorize]
public ActionResult Tutorial()
{
ViewBag.HtmlStr = "Workstation name: " + IP();
return View();
}
[Authorize]
public ActionResult Contact()
{
ViewBag.HtmlStr = "Workstation name: " + IP();
return View();
}
public string IP()
{
string myHost = System.Net.Dns.GetHostName();
string myIP = null;
for (int i = 0; i <= System.Net.Dns.GetHostEntry(myHost).AddressList.Length - 1; i++)
{
if (System.Net.Dns.GetHostEntry(myHost).AddressList[i].IsIPv6LinkLocal == false)
{
myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString();
}
}
return myIP;
}
layout.cshtml
<section id="login">
<p>@ViewBag.HtmlStr</p>
</section>
I feel like im repeating myself adding the same section of code each time, then having to call the method and code into other controllers, since the layout calls the view bag from each controller method.
I feel like there is bound to be a better way, hence i wanted the IP assign name in the layout master and have it set once. I dunno how to go about doing it since layout, is well for layout.Also ive googled the hell out it haha
Upvotes: 2
Views: 3504
Reputation: 10211
Why don't you use an actionFilter? Seems a typical scenario of cross cutting concern, so:
public class IPActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
ViewBag.HtmlStr = "Workstation name: " + IP();
}
}
and on your action:
[IPActionFilter]
[Authorize]
public ActionResult Index()
{
return View();
}
or better, decorate your controller to have the feature on every method of controller:
[IPActionFilter]
public class HomeController : Controller
Upvotes: 1
Reputation: 5771
If your objective is to show the Workstation IP address you should be using
@Request.UserHostAddress
Place this in your Layout page. When running this on localhost, you may see ::1, but when deployed on a web server, you will see the correct results.
If you need to show a mapping name, you could do the below
In your layout add
<div id="machine"></div>
Also have a js to fetch the name mapping.
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: "Home/Machine"
}).done(function (data) {
$('#machine').text(data);
});
});
</script>
Finally in your Home Controller
public JsonResult Machine()
{
string ip = Request.UserHostAddress;
//Find the mapped name and return.
//Need to add your logic here.
return Json("Head Board Assembly", JsonRequestBehavior.AllowGet);
}
Upvotes: 3
Reputation: 126
It seems you want to create a static helper method of some sorts that not only gets the IP address but also does some custom logic. Then you can call it anywhere in the layout.
"Say if it was 192.168.7.12 then that workstation would have the name "Head Board Assembly"
namespace MyApp.Helpers
{
public static class GetIP
{
public static string AssignedIPName()
{//do custom logic
}
}
}
Welcome @MyApp.Helpers.GetIP.AssignedIPName().
Upvotes: 3
Reputation: 6832
An easy way would be to extend WebViewPage
to add your own extra properties.
public abstract class BaseViewPage<TModel> : WebViewPage<TModel>
{
public string SomeProperty
{
get
{
return someValue;
}
}
}
Next find this line in your Views
folder's web.config
file :
<pages pageBaseType="System.Web.Mvc.WebViewPage">
And change it to your newly created BaseViewPage
:
<pages pageBaseType="Your.App.Namespace.BaseViewPage">
And then you'll be able to use it inside any view file by simple calling @SomeProperty
or @base.SomeProperty
. Both do the same, but I prefer the latter for clarity's sake.
Upvotes: 2