Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Wednesday, October 18, 2017

VS 2017 MVC Core 2.0 error - Cannot find reference assembly 'Microsoft.AspNetCore.Mvc.Razor.ViewCompilation-x86.exe' file for package Microsoft.AspNetCore.Mvc.Razor.ViewCompilation-x86

Scenario

After compiling a MVC Core 2.0 project in VS 2017, the browser isn't displaying the site

Error
An unhandled exception occurred while processing the request.

Cannot find reference assembly 'Microsoft.AspNetCore.Mvc.Razor.ViewCompilation-x86.exe' file for package Microsoft.AspNetCore.Mvc.Razor.ViewCompilation-x86

Solution
Open the .csproj file in a text editor
In the section set the MvcRazorCompileOnPublish tag as true:
      <mvcrazorcompileonpublish>true</mvcrazorcompileonpublish>

Tuesday, October 17, 2017

VS 2017 MVC Core 2.0 publish fails

Scenario
Publish a MVC Core 2.0 project in Visual Studio 2017

Error
Severity
Error
Code
The command ""bin\Release\net461\Microsoft.AspNetCore.Mvc.Razor.ViewCompilation-x86.exe"
Description
@"obj\Release\net461\microsoft.aspnetcore.mvc.razor.viewcompilation.rsp"" exited with code 1


Solution
Open the .csproj file in a text editor
In the section set the MvcRazorCompileOnPublish tag as false:
      <mvcrazorcompileonpublish>false</mvcrazorcompileonpublish>

Tuesday, October 10, 2017

MVC Core - Duplicate Content Error after Upgrading Visual Studio 2017

Error

  • Dotnet-aspnet-codegenerator-design has stopped working
  • Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default.
  • Failed to build the project. Please fix the build errors and retry again


Reason

The problem is a change on the RC3 release of Visual Studio 2017, if you have globs in your project and you try to build it using the newest SDK, you'll get the error. Basically your content, such as the files in wwwroot are now getting imported by the default includes that are built-in to Visual Studio and by the manual rules in your csproj file.


Solution 1

Open your web project file .csproj and remove the duplicated items specified in the error message


Basically remove the ItemGroup tag with all the wwwroot paths.



Solution 2

Open your web project file .csproj
Look for the tag
Add the next tag false


Monday, June 12, 2017

ASP MVC Error Unable to connect to web server IIS Express

If you get the next error when trying to run your app from visual studio:


You can try different things to fix the problem

- Right click on your project, then select properties, click on the Debug section, assign a new port in the App URL direction



- In explorer go to the path \.vs\config and delete the applicationhost.config, then rebuild your project 



- Open the task manager and stop the IIS Express if is running so you can get a fresh instance


Tuesday, February 23, 2016

MVC error on IIS 7 and 7.5 The Web server is configured to not list the contents of this directory.

If you get the error:The Web server is configured to not list the contents of this directory.

It is because the MVC URLs end without .aspx, these will not be picked up by IIS and run through the intergrated pipeline and therefore you will end up with 404 not found errors.
Another possible cause is you are using a .Net 4.5 application on a server where it was installed after IIS.

To solve this problem consider how much traffic you will have on your site, if you are not going to have a lot of traffic use the attribute runAllManagedModulesForAllRequest with true value, but keep in mind all your registered HTTP modules run on every request, not just managed requests (e.g. .aspx). This means modules will run on ever .jpg .gif .css .html .pdf etc.

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

But if you are going to have a lot of traffic it is better to add the routing module:

<system.webServer>
    <modules>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" 
           type="System.Web.Routing.UrlRoutingModule" 
            preCondition="" />
    </modules>
  </system.webServer>

Another solution is to install a patch from Microsoft that enables certain IIS 7.0 or IIS 7.5 handlers to handle requests whose URLs do not end with a period.
This is the link to download this patch:

https://support.microsoft.com/en-us/kb/980368


Wednesday, September 24, 2014

ASP.NET MVC Razor Unrequired property keeps getting data-val-required attribute

If you have the problem that your text field validation gets generated althoug there is no Required attribute set on property, use the next code in the Application_Start to force the compiler to validate only if there is a required attribute on the property:

// Whit this line of code you will remove data-val-required DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

// With these two lines you will remove data-val-number and data-val ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());


Thursday, September 11, 2014

ASP.NET MVC Razor Override "id" and “name” attribute with TextBoxFor

Use the code below as example to override the id or/and name attributes with TextBoFor, something really important to keep in mind is that "id" and "Name" is case sensitive.
    
   @Html.TextBoxFor(x => x.Data, new { Name = Model.Name + "_Custom", id = Model.ID + "_Custom" })


Thursday, August 7, 2014

How to link to a root controller from area controller in MVC

Sometimes you need to call a root controller from inside of an area, usually when you are inside of an Admin console for example.
The process is really simple, all you have to do is specify the area as blank in the RenderAction or the RedirectToAction functions.



        <% Html.RenderAction("Action", "Controller", new { area = "" }); %>


        public ActionResult redirect()
        {
            return RedirectToAction("Action", "Controller", new { area = "" });

            return RedirectToAction("Action", "Controller", new { area = "", id = 5 });

        }


Thursday, July 3, 2014

MVC razor decode / encode HTML

Razor has the ability to automatically encoded html.
In order to avoid encoding use the HTML helper: @Html.Raw(text).



@{ var br = "
";} This is the line number 1 @Html.Raw(br) This is the line number 2 @Html.Raw(br) This is the line number 3 @Html.Raw(br)

Thursday, June 12, 2014

Steps to set startup page for debugging in asp.net mvc aplication

To set the start page in Visual Studio for a MVC application, follow the next steps:

  1. Got to Projects >> Properties...
  2. Click the Web Tab
  3. Select the Specific Page radio button
  4. Type the URL in the Specific Page text box


Monday, June 9, 2014

ModelState.AddModelError to add a simple validation

Use ModelState.AddModelError to add a simple validation and make the ModelState.isValid with errors.



   // Validation logic
   if (MyData.Email.Trim().Length == 0){
        ModelState.AddModelError("Email", "Invalid Email");
   
        return View(MyData);
   }


Tuesday, June 3, 2014

Using @Html.ActionLink as image link

If you need to serve image with the link using Razor, you can use HtmlHelpers like Html.ActionLink() to make it possible, this is how you can do it. First define the style with the image:

a.link_logo {
    background:url(/images/logo.jpg) no-repeat top left;
    width: 355px;
    height: 75px;
    display: block;
    text-indent: -9999px; /* In order to hide the text */
}



Then use the HtmlHelper and add the style:

// --------------------------------
@Html.ActionLink("Home", "Index", "Home", null, null, null, null, new { @class = "link_logo" })


// --------------------------------
@Html.ActionLink("Home", "Index", null, new { @class = "link_logo" })


//---------------------------------