MVC etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
MVC etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

26 Mayıs 2011 Perşembe

Razor render- MVC3 View Render to String

        public static string RazorRender(Controller context, string DefaultAction)

        {

            string Cache = string.Empty;

 

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            System.IO.TextWriter tw = new System.IO.StringWriter(sb);

 

            RazorView view_ = new RazorView(context.ControllerContext, DefaultAction, null, false, null);

            view_.Render(new ViewContext(context.ControllerContext, view_, new ViewDataDictionary(), new TempDataDictionary(), tw), tw);

 

            Cache = sb.ToString();

 

            return Cache;

        }

 

        public static string RenderRazorViewToString(string viewName, object model)

        {

            ViewData.Model = model;

            using (var sw = new StringWriter())

            {

                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);

                var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);

                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();

            }

        }

 

        public static class HtmlHelperExtensions

        {

            public static string RenderPartialToString(ControllerContext context, string partialViewName, ViewDataDictionary viewData, TempDataDictionary tempData)

            {

                ViewEngineResult result = ViewEngines.Engines.FindPartialView(context, partialViewName);

 

                if (result.View != null)

                {

                    StringBuilder sb = new StringBuilder();

                    using (StringWriter sw = new StringWriter(sb))

                    {

                        using (HtmlTextWriter output = new HtmlTextWriter(sw))

                        {

                            ViewContext viewContext = new ViewContext(context, result.View, viewData, tempData, output);

                            result.View.Render(viewContext, output);

                        }

                    }

 

                    return sb.ToString();

                }

 

                return String.Empty;

            }

        }

 

9 Ekim 2010 Cumartesi

MVC Page URL Relative

<link href="<%= 
Page.ResolveUrl("
~/Content/core/css/smoothness/jquery-ui-1.8.1.custom.css") %>"
rel="stylesheet"
type="text/css" />
<link href='<%=
Url.Content("~/Plugins/Views/Webilan/Content/Main.css") %>'
rel="stylesheet"
type="text/css">
<link rel="stylesheet" type="text/css" media="screen"
href
='<%= VirtualPathUtility.ToAbsolute(
string.Format("~/Content/{0}/css/Site.css",
PostSystem.ThemeName )) %
>' />

7 Temmuz 2010 Çarşamba

XML node

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlFile.NameTable);
nsmgr.AddNamespace(
"smap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0");
string xpath
= "//smap:siteMapNode[@url=\"{0}\"]";
string url
= "~/Index";

return GetSiteMapNodeFromElement((XmlElement)xmlFile.SelectSingleNode(String.Format(xpath, url), nsmgr));

26 Mayıs 2010 Çarşamba

Dynamic sitemap in ASP.NET MVC

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class MVCUrlAttribute : ActionFilterAttribute
{
public string Url { get; private set; }

public MVCUrlAttribute(string url)
{
this.Url = url;
}

public override void OnResultExecuting(ResultExecutingContext filterContext)
{
// Put this 'canonical url' into the model (which feeds the view)
// to help search engines with issues of duplicate content
filterContext.Controller.ViewData["CanonicalUrl"] = url;
base.OnResultExecuting(filterContext);
}
}


// Find all the MVC Routes
Log.Debug("*** FINDING ALL MVC ROUTES MARKED FOR INCLUSION IN SITEMAP");
var allControllers
= Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(Controller)));
Log.DebugFormat(
"Found {0} controllers", allControllers.Count());

foreach (var controllerType in allControllers)
{
var allPublicMethodsOnController
= controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
Log.DebugFormat(
"Found {0} public methods on {1}", allPublicMethodsOnController.Count(), controllerType.Name);

foreach (var publicMethod in allPublicMethodsOnController)
{
var mvcurlattr
= publicMethod.GetCustomAttributes(true).OfType<MVCUrlAttribute>().FirstOrDefault();
if (mvcurlattr != null)
{
string url = mvcurlattr.Url;
Log.Debug(
"Found " + controllerType.Name + "." + publicMethod.Name + " <-- " + url);
Global.SiteMapUrls.Add(url);
//<-- your code here using url
}
}
}


 



 



 




protected string GetUrl(object routeValues)
{
RouteValueDictionary values
= new RouteValueDictionary(routeValues);
RequestContext context
= new RequestContext(HttpContext, RouteData);

string url = RouteTable.Routes.GetVirtualPath(context, values).VirtualPath;

return new Uri(Request.Url, url).AbsoluteUri;
}

List
<string> urlList = new List<string>();
urlList.Add(GetUrl(
new { controller = "Help", action = "Edit" }));
urlList.Add(GetUrl(
new { controller = "Help", action = "Create" }));
urlList.Add(GetUrl(
new { controller = "About", action = "Company" }));
urlList.Add(GetUrl(
new { controller = "About", action = "Management" }));

23 Mayıs 2010 Pazar

MVC business layer

public void Save(EmployeeViewModel employeeViewModel)
{
var employee
= (from emp in dataContext.Employees
where emp.EmployeeID == employeeViewModel.EmployeeID
select emp).SingleOrDefault();


if (employee != null)
{

employee.Address
= employeeViewModel.Address;
employee.Salary
= employeeViewModel.Salary;
employee.Title
= employeeViewModel.Title;
}
dataContext.SubmitChanges();
}

public void Save(EmployeeViewModel employeeViewModel)
{

var employee
= (from emp in dataContext.Employees
where emp.EmployeeID == employeeViewModel.EmployeeID
select emp).SingleOrDefault();

if (employee != null)
{
ModelCopier.CopyModel(employeeViewModel, employee);
}
dataContext.SubmitChanges();

}


 



refer :http://weblogs.asp.net/rajbk/archive/2010/03/31/easy-way-to-update-models-in-your-asp-net-mvc-business-layer.aspx

22 Mayıs 2010 Cumartesi

What is MEF?

 

The Managed Extensibility Framework (or MEF for short) simplifies the creation of extensible applications. MEF offers discovery and composition capabilities that you can leverage to load application extensions.

What problems does MEF solve?

MEF presents a simple solution for the runtime extensibility problem. Until now, any application that wanted to support a plugin model needed to create its own infrastructure from scratch. Those plugins would often be application-specific and could not be reused across multiple implementations.
    MEF provides a standard way for the host application to expose itself and consume external extensions. Extensions, by their nature, can be reused amongst different applications. However, an extension could still be implemented in a way that is application-specific. Extensions themselves can depend on one another and MEF will make sure they are wired together in the correct order (another thing you won't have to worry about). MEF offers a set of discovery approaches for your application to locate and load available extensions. MEF allows tagging extensions with additonal metadata which facilitates rich querying and filtering</LI>

How does MEF work?

Roughly speaking, MEF's core is comprised of a catalog and a CompositionContainer. A catalog is responsible for discovering extensions and the container coordinates creation and satisfies dependencies.
    MEF's first-class citizen is a ComposablePart (see Parts). A composable part offers up one or more Exports, and may also depend on one or more externally provided services or Imports. A composable part also manages an instance, which can be an object instance of a given type (it is in the default MEF implementation). MEF, however, is extensible and additonal ComposablePart implementations can be provided as long as they adhere to the Import/Export contracts. Exports and imports each have a Contract. Contracts are the bridge between exports and imports. An export contract can consist of further metadata that can be used to filter on its discovery. For example, it might indicate a specific capability that the export offers. MEF's container interacts with Catalogs to have access to composable parts. The container itself resolves a part's dependencies and exposes Exports to the outside world. You're free to add composable part instances directly to the container if you wish. A ComposablePart returned by a catalog will likely be an extension to your application. It might have Imports (dependencies) on components the host application offers, and it's likely to Export others. The default MEF composable part implementation uses attribute-based metadata to declare exports and imports. This allows MEF to determine which parts, imports, and exports are available completely throug